Trigger insert old values- values that was updated

前端 未结 5 644
再見小時候
再見小時候 2020-12-08 09:57

I need to create trigger in SQL Server 2008 that gone insert all values from one row in which some value was changed into Log table!

For example if i have table Empl

相关标签:
5条回答
  • 2020-12-08 10:01

    In SQL Server 2008 you can use Change Data Capture for this. Details of how to set it up on a table are here http://msdn.microsoft.com/en-us/library/cc627369.aspx

    0 讨论(0)
  • 2020-12-08 10:05

    Here's an example update trigger:

    create table Employees (id int identity, Name varchar(50), Password varchar(50))
    create table Log (id int identity, EmployeeId int, LogDate datetime, 
        OldName varchar(50))
    go
    create trigger Employees_Trigger_Update on Employees
    after update
    as
    insert into Log (EmployeeId, LogDate, OldName) 
    select id, getdate(), name
    from deleted
    go
    insert into Employees (Name, Password) values ('Zaphoid', '6')
    insert into Employees (Name, Password) values ('Beeblebox', '7')
    update Employees set Name = 'Ford' where id = 1
    select * from Log
    

    This will print:

    id   EmployeeId   LogDate                   OldName
    1    1            2010-07-05 20:11:54.127   Zaphoid
    
    0 讨论(0)
  • 2020-12-08 10:07
        createTRIGGER [dbo].[Table] ON [dbo].[table] 
    FOR UPDATE
    AS
        declare @empid int;
        declare @empname varchar(100);
        declare @empsal decimal(10,2);
        declare @audit_action varchar(100);
        declare @old_v varchar(100)
    
        select @empid=i.Col_Name1 from inserted i;  
        select @empname=i.Col_Name2  from inserted i;   
        select @empsal=i.Col_Name2 from inserted i;
        select @old_v=d.Col_Name from deleted d
    
        if update(Col_Name1)
            set @audit_action='Updated Record -- After Update Trigger.';
        if update(Col_Name2)
            set @audit_action='Updated Record -- After Update Trigger.';
    
        insert into Employee_Test_Audit1(Col_name1,Col_name2,Col_name3,Col_name4,Col_name5,Col_name6(Old_values)) 
        values(@empid,@empname,@empsal,@audit_action,getdate(),@old_v);
    
        PRINT '----AFTER UPDATE Trigger fired-----.'
    
    0 讨论(0)
  • 2020-12-08 10:08

    ALTER trigger ETU on Employee FOR UPDATE AS insert into Log (EmployeeId, LogDate, OldName) select EmployeeId, getdate(), name from deleted go

    0 讨论(0)
  • 2020-12-08 10:20

    In your trigger, you have two pseudo-tables available, Inserted and Deleted, which contain those values.

    In the case of an UPDATE, the Deleted table will contain the old values, while the Inserted table contains the new values.

    So if you want to log the ID, OldValue, NewValue in your trigger, you'd need to write something like:

    CREATE TRIGGER trgEmployeeUpdate
    ON dbo.Employees AFTER UPDATE
    AS 
       INSERT INTO dbo.LogTable(ID, OldValue, NewValue)
          SELECT i.ID, d.Name, i.Name
          FROM Inserted i
          INNER JOIN Deleted d ON i.ID = d.ID
    

    Basically, you join the Inserted and Deleted pseudo-tables, grab the ID (which is the same, I presume, in both cases), the old value from the Deleted table, the new value from the Inserted table, and you store everything in the LogTable

    0 讨论(0)
提交回复
热议问题