Trigger insert old values- values that was updated

前端 未结 5 643
再見小時候
再見小時候 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: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
    

提交回复
热议问题