简体   繁体   English

SQL Server触发器检查列值是否已更改为插入

[英]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. 我大约有25个表,每个表都有3个触发器(插入,更新,删除)以生成有关用户操作的日志。 All these tables get updated each time there is an edit in the UI, though their value is not changed in the UI. 每次在UI中进行编辑时,所有这些表都会更新,尽管它们的值不会在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. 您可以使用deleted表找出旧值,然后inserted新值。 Also note that INSERT, UPDATE and DELETE may affect more than one rows, so you trigger should handled that. 另请注意,INSERT,UPDATE和DELETE可能影响多个行,因此您应由触发器处理。

below trigger code will gives you list of rows for any change in deptNotes. 下面的触发代码将为您提供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 如果您的deptNotes可能包含NULL值,则需要使用ISNULL()来处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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