Hello Santosh < @Santosh Umarani >
If all you need is to INERT new rows or UPDATE existing rows then you can use MERGE as other mentioned, but if you also need to log the changes as you asked for or if you need to actually trigger a push action when the excel changed o the table in the SQL Server changed, then check the below explanation
whenever value has to be modified
There are two approach to trigger actions: (1) Scheduled Pull: you connect to the source every X time and check if there was a changed. (2) Push: The source trigger an action whenever there is a changed. In order to use push approach (option 2), the source must support this type of trigger. I will come back to this according to the requirement of your needs...
Regarding your needs, some clarification is needed
add it in one collection.
Not sure what you mean by "collection". Collection of what and where? SQL Server does not manage collections but tables.
after updating the DB, I have to write only modified row in log.
If the by "log" you mean a table then this is a simple action which can be done using push approach, since SQL Server supports executing of query as a result of UPDATE values in table. This is done using object named TRIGGER.
For example if the table which include the data named ProjectsTbl and your log table named LogTbl, then you can CREATE TRIGGER on ProjectsTbl after UPDATE and in the TRIGGER you INSERT the values that were changed to the LogTbl.
Check this full demo:
use tempdb
GO
DROP TABLE IF EXISTS dbo.ProjectsTbl, dbo.LogTbl
GO
CREATE TABLE dbo.ProjectsTbl(
id int identity(1,1),
rojectName NVARCHAR(100),
Mig BIT,
Conv BIT,
Upl BIT
)
INSERT dbo.ProjectsTbl (rojectName, Mig, Conv, Upl)
VALUES ('Ronen',1,0,1),('Ariely',0,1,1)
GO
CREATE TABLE dbo.LogTbl(
id int identity(1,1),
rojectName NVARCHAR(100),
Mig BIT,
Conv BIT,
Upl BIT,
ChangedAt DATETIME2
)
GO
-- Option 1: log only if the update include one of the columns Mig, Conv, Upl
-- note that if you UPADTE value to the old value then this considered as updated
CREATE OR ALTER TRIGGER LogUpdatesInProjectsTbl ON dbo.ProjectsTbl AFTER UPDATE AS
-- Note! You can use UPDATE() function or COLUMNS_UPDATED function in the body of the trigger, if you want to execute the trigger only if specific column(s) where changed
-- COLUMNS_UPDATED: https://learn.microsoft.com/en-us/sql/t-sql/functions/columns-updated-transact-sql?view=sql-server-ver15&WT.mc_id=DP-MVP-5001699
-- UPDATE() : https://learn.microsoft.com/sql/t-sql/functions/update-trigger-functions-transact-sql?view=sql-server-ver15&WT.mc_id=DP-MVP-5001699
IF ( UPDATE (Mig) OR UPDATE(Conv) or UPDATE(Upl) ) BEGIN
INSERT dbo.LogTbl (rojectName, Mig, Conv, Upl, ChangedAt)
SELECT rojectName, Mig, Conv, Upl, GETDATE() FROM deleted -- We log the values before the UPDATE
END
GO
-- Option 2: log only if the actual bvalue was changed on one of the columns Mig, Conv, Upl
CREATE OR ALTER TRIGGER LogUpdatesInProjectsTbl ON dbo.ProjectsTbl AFTER UPDATE AS
-- Note! This trigger check if the actual values where changed and not jsut if the value was UPDATE
--select * from deleted
--select * from inserted
INSERT dbo.LogTbl (rojectName, Mig, Conv, Upl, ChangedAt)
SELECT d.rojectName, d.Mig, d.Conv, d.Upl, GETDATE()
FROM deleted d -- We log the values before the UPDATE
LEFT JOIN inserted i on d.id = i.id
WHERE d.Mig != i.Mig or d.rojectName !=i.rojectName or d.Upl != i.Upl
GO
SELECT * FROM dbo.ProjectsTbl
SELECT * FROM dbo.LogTbl
GO -- Log empty
-- Test UPDATE ingle row
UPDATE dbo.ProjectsTbl
SET Mig = 1
WHERE id = 2
GO
-- Check result:
SELECT * FROM dbo.ProjectsTbl
SELECT * FROM dbo.LogTbl
GO -- Notice a new log row
-- Inser more rows for tests
INSERT dbo.ProjectsTbl (rojectName, Mig, Conv, Upl)
VALUES ('Ronen2',0,0,0),('Ariely2',0,0,0)
GO
SELECT * FROM dbo.ProjectsTbl
SELECT * FROM dbo.LogTbl
GO
-- Test UPDATE of multiple rows
UPDATE dbo.ProjectsTbl
SET Mig = 1
WHERE id > 1 -- three rows
GO
-- Check result:
SELECT * FROM dbo.ProjectsTbl
SELECT * FROM dbo.LogTbl
GO -- Notice that we updated three rows but only two new rows in the log, since one of the updated rows was nopt actually changed
I am uploading a excel file
Not clear to me if you want to make the check and log when your excel changed or when you upload data from the excel.
Above solution is in the SQL Server level which mean it is not related to the excel specifically and will happen on any UPDATE in the SQL Server level. If you need to check changes in the Excel then the solution is not related to SQL Server.
In this case, Step one of your request is to trigger action "whenever value has to be modified"
in the excel, and Step two will be to to log these changed rows.
Excel also include option for push actions when value is changed. This can be done using Macro
and the event Worksheet_Change
.
Check this document for a demo: