简体   繁体   中英

SQL Server trigger to check if column value was changed to insert

I have around 25 tables that each have 3 triggers (insert, update, delete) to generate logs on user actions. All these tables get updated each time there is an edit in the UI, though their value is not changed in the UI.

In such case I can't just check for if a column is updated, I need to check if previous updated value is not the same as the newly updated value so then I can insert to my log table. Also, I need to insert all updated table values to log table as a string separated by comma.

Is there any efficient way how to handle this?

ALTER TRIGGER [dbo].[Activity_Update]
ON [dbo].[Activity]
AFTER UPDATE
AS
BEGIN
    DECLARE @Names VARCHAR(8000) 
    SET @result = ' '

    DECLARE @id INT;
    SELECT @id = i.ID FROM inserted i;      

    IF UPDATE(deptNotes) 
    BEGIN
        DECLARE @OldValue NVARCHAR(MAX)

        SELECT TOP 1 @OldValue = deptNotes 
        FROM Activity 
        WHERE id = @id 
        ORDER BY Timestamp DESC

        IF (@OldValue != (SELECT i.deptNotes FROM inserted i))        
        BEGIN
            IF ((SELECT i.deptNotes FROM inserted i) != ' ')
               SELECT @result =  @result + ',' + 'Modified dept. Notes'
            ELSE
               SELECT @result =  @result + ',' + 'Removed dept. Notes'
        END
   END

   IF UPDATE(deptActivityID) 
   BEGIN
       DECLARE @OldValue1 NVARCHAR(MAX)

       SELECT TOP 1 @OldValue1 = deptActivityID 
       FROM Activity 
       WHERE id = @id  
       ORDER BY Timestamp DESC

       IF (@OldValue1 != (SELECT i.deptActivityID FROM inserted i))           
       BEGIN
           SELECT @result =  @result + ',' + 'Changed dept. Activity  ' 
       END
    END

    IF UPDATE(SubmissionDate) 
    BEGIN
    declare @OldValue2 nvarchar(max)
    select Top 1 @OldValue2 = submissiondate from [Activity] where id=@id Order by Timestamp DESC

     If (@OldValue2 != (select i.submissiondate from inserted i))         
     BEGIN
     Select @result =  @result + ',' + 'Changed application date - ' + '"' + (select cast(i.submissiondate as nvarchar(500)) from inserted i)+ '"' 
     END
     END


     INSERT [Activity]
     ( 
       [deptActivityID], 
       [deptNotes], 
       [SubmissionDate], 
       [Username], 
       [Operation], 
       [Comment]
       )
       SELECT 
       v.deptActivityID, 
       v.deptNotes, 
       v.SubmissionDate,
       v.[LastEditBy],
       'update', 
       @result
       FROM inserted v
       END
       GO

You can use the deleted table to find out what was the old value and inserted for the new value. Also note that INSERT, UPDATE and DELETE may affect more than one rows, so you trigger should handled that.

below trigger code will gives you list of rows for any change in deptNotes.

select  'deptActivityID ' + d.deptActivityID +
        ' Changed from ' + d.deptNotes + ' to ' + i.deptNotes
from    deleted d
        inner join inserted i   on  d.deptActivityID    = i.deptActivityID
where   d.deptNotes <> i.deptNotes

If your deptNotes may contains NULL value, you need to use ISNULL() to handle that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM