获取更新的列值和旧值ON更新,插入触发器

时间:2016-12-12 07:30:31

标签: sql triggers

我是Triggers的新手。我有'Sample'和'SampleAudit'表。我创建了Trigger 用于更新和插入。触发器如下。

CREATE TRIGGER [ss].[SampleTrigger]
   ON  [ss].[Sample] 
   FOR INSERT,UPDATE

AS 
    declare @fullname varchar(200);
    declare @addressline1 varchar(200);
    declare @addressline2 varchar(200);
    declare @landmark varchar(200);
    declare @city bigint;
    declare @state bigint;
    declare @pin varchar(50);
    declare @country varchar(100);
    declare @auditaction varchar(100);


    select @fullname=i.fullname from inserted i;
    select @addressline1=i.addressline1 from inserted i;
    select @addressline2=i.addressline2 from inserted i;
    select @landmark=i.landmark from inserted i;
    select @city=i.city from inserted i;
    select @state=i.state from inserted i;
    select @pin=i.pin from inserted i;
    select @country=i.country from inserted i;

   if UPDATE(@fullname)
    set @auditaction= 'updated FullName from'+oldval+' to '+ new value ;

    if UPDATE(@addressline1)
    set @auditaction= 'updated addressline1 from'+oldval+' to '+ new value ;

    if UPDATE(@addressline2)
    set @auditaction= 'updated addressline2 from'+oldval+' to '+ new value ;

    if UPDATE(@landmark)
    set @auditaction= 'updated landmark from'+oldval+' to '+ new value ;



insert into [ss].[SampleAudit]
(FullName,AddressLine1,AddressLine2,LandMark,City,State,Pin,Country,auditaction)
values(@fullname,@addressline1,@addressline2,@landmark,@city,@state,@pin,@country,@auditaction)

这里每次更新新的raw都会插入到Audit表中。所以

    if Update(fullname)
    if UPDATE(@addressline1)  
    if UPDATE(@addressline2)  
    if UPDATE(@landmark)

所有人都在更新。那么如何才能更新哪些字段?所有更新字段的旧值和新值。

1. I need to track which and all fields updated.
2. and from which value to which value it is updated ?

1 个答案:

答案 0 :(得分:0)

CREATE TRIGGER [ss].[SampleTrigger]
   ON  [ss].[Sample] 
   FOR INSERT,UPDATE

AS 
    declare @fullname varchar(200);
    declare @addressline1 varchar(200);
    declare @addressline2 varchar(200);
    declare @landmark varchar(200);
    declare @city bigint;
    declare @state bigint;
    declare @pin varchar(50);
    declare @country varchar(100);
    declare @auditaction varchar(100);


    select @fullname=i.fullname from inserted i;
    select @addressline1=i.addressline1 from inserted i;
    select @addressline2=i.addressline2 from inserted i;
    select @landmark=i.landmark from inserted i;
    select @city=i.city from inserted i;
    select @state=i.state from inserted i;
    select @pin=i.pin from inserted i;
    select @country=i.country from inserted i;

    Declare @Type int
    IF EXISTS (SELECT * FROM inserted) and 
        EXISTS (SELECT * FROM deleted)
               SELECT @Type = 'U'
    Else
         SELECT @Type = 'I'

    if (@Type = 'U')
    Begin
        set @auditaction= 'updated FullName from'+ (Select d.fullname From  deleted  d) +' to '+ (Select i.fullname from inserted i) ;
        insert into [ss].[SampleAudit] (FullName,AddressLine1,AddressLine2,LandMark,City,State,Pin,Country,auditaction)
        values(@fullname,@addressline1,@addressline2,@landmark,@city,@state,@pin,@country,@auditaction)

        set @auditaction= 'updated addressline1 from'+(Select d.addressline1 From   deleted  d)+' to '+ (Select i.addressline1 from inserted i) ;
        insert into [ss].[SampleAudit] (FullName,AddressLine1,AddressLine2,LandMark,City,State,Pin,Country,auditaction)
        values(@fullname,@addressline1,@addressline2,@landmark,@city,@state,@pin,@country,@auditaction)

        set @auditaction= 'updated addressline1 from'+(Select d.addressline2 From   deleted  d)+' to '+ (Select i.addressline2 from inserted i) ;
        insert into [ss].[SampleAudit] (FullName,AddressLine1,AddressLine2,LandMark,City,State,Pin,Country,auditaction)
        values(@fullname,@addressline1,@addressline2,@landmark,@city,@state,@pin,@country,@auditaction)

        set @auditaction= 'updated addressline1 from'+(Select d.landmark From   deleted  d)+' to '+ (Select i.landmark from inserted i) ;
        insert into [ss].[SampleAudit] (FullName,AddressLine1,AddressLine2,LandMark,City,State,Pin,Country,auditaction)
        values(@fullname,@addressline1,@addressline2,@landmark,@city,@state,@pin,@country,@auditaction)
    End

    If (@Type= 'I')
    Begin
        Set @auditaction = 'Insert'
        insert into [ss].[SampleAudit] (FullName,AddressLine1,AddressLine2,LandMark,City,State,Pin,Country,auditaction)
        values(@fullname,@addressline1,@addressline2,@landmark,@city,@state,@pin,@country,@auditaction)
    End