触发记录插入/更新/删除的值SQL Server 2016

时间:2017-07-07 07:45:50

标签: sql-server tsql

应用程序向表X添加/更新/删除行。下面的示例。

|ID|Type*|Name |Value|Description
|1 |1    |Mike |100  |-
|2 |1    |John |50   |-
|3 |1    |Vince|10   |-

*列类型= 1 - 这是来自应用程序的数据

列类型= 2 - 这将是日志数据

我还需要触发器来记录表X中的插入/更新/删除值。

例如,当更改应用程序行号2和3时:

|ID|Type|Name  |Value|Description
|2 |1   |Monica|60   |-
|3 |1   |Tom   |5    |-

表X的结果应该是这样的:

|ID|Type|Name  |Value|Description
|1 |1   |Mike  |100  |-
|2 |1   |Monica|60   |-
|3 |1   |Tom   |5    |-
|4 |2   |Monica|60   |UPDATE OPERATION
|5 |2   |Tom   |5    |UPDATE OPERATION

当应用程序添加另一行时:

|ID|Type|Name  |Value|Description
|6 |1   |Paul  |200  |-

表X的结果应该是这样的:

|ID|Type|Name  |Value|Description
|1 |1   |Mike  |100  |-
|2 |1   |Monica|60   |-
|3 |1   |Tom   |5    |-
|4 |2   |Monica|60   |UPDATE OPERATION
|5 |2   |Tom   |5    |UPDATE OPERATION
|6 |1   |Paul  |200  |-
|7 |2   |Paul  |200  |INSERT OPERATION

...

2 个答案:

答案 0 :(得分:1)

我认为最直接的方法是通过instead of触发器,并为每个操作使用单独的触发器。我认为这些都是正确的:

create table X(ID int not null,Type int not null, Name varchar(11) not null, 
               Value int not null, Description varchar(38) not null);
insert into X(ID,Type,Name,Value,Description) values
(1,1,'Mike' ,100,'-'),
(2,1,'John' ,50 ,'-'),
(3,1,'Vince',10 ,'-');
go
create table Numbers (n int not null);
insert into Numbers(n) values (1),(2),(3); --TODO - May need more in future
go
create trigger T_X_I on X instead of insert
as
    set nocount on;
    insert into X(ID,Type,Name,Value,Description)
    select i.ID,n.n,i.Name,i.Value,
       CASE WHEN n.n = 1 THEN i.Description ELSE 'INSERT' END
    from inserted i
        cross join
        Numbers n
    where n.n in (1,2);
go
create trigger T_X_U on X instead of update
as
    set nocount on;
    merge into X
    using (select * from inserted i cross join Numbers n where n.n in (1,2)) s
    on
        s.ID = X.ID and
        s.n = 1 and
        X.Type = 1
    when matched then update
       set Name = s.Name,Value = s.Value,Description = s.Description
    when not matched then
       insert (ID,Type,Name,Value,Description) values 
              (s.ID,s.n,s.Name,s.Value,'UPDATE');
go
create trigger T_X_D on X instead of delete
as
    set nocount on;
    merge into X
    using (select * from deleted d cross join Numbers n where n.n in (1,2)) s
    on
        s.ID = X.ID and
        s.n = 1 and
        X.Type = 1
    when matched then delete
    when not matched then
      insert (ID,Type,Name,Value,Description) values 
             (s.ID,s.n,s.Name,s.Value,'DELETE');
go
update X set
    Name = CASE WHEN ID=2 THEN 'Monica' ELSE 'Tom' END,
    Value = CASE WHEN ID=2 THEN 60 ELSE 5 END
where ID in (2,3)
go
insert into X (ID,Type,Name,Value,Description)
values (6,1,'Paul',200,'-')
go
select * from X

结果:

ID          Type        Name        Value       Description
----------- ----------- ----------- ----------- --------------------------------------
1           1           Mike        100         -
2           1           Monica      60          -
3           1           Tom         5           -
2           2           Monica      60          UPDATE
3           2           Tom         5           UPDATE
6           1           Paul        200         -
6           2           Paul        200         INSERT

如果您已有Numbers表或同等信息,请随意在上述查询中替换它。

答案 1 :(得分:0)

你面临什么问题?。

这些触发器非常简单:

.../project/public

这样的事情不适合你吗?。

顺便说一句,WHERE TYPE = 1;阻止重新记录您的日志记录操作。