首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这个触发器如何实现呢

2012-12-17 
这个触发器怎么实现呢做更新操作的时候,假如字段 num 更新为2 就往另一个表 change里面插入这条更新后的数

这个触发器怎么实现呢
做更新操作的时候,假如字段 num 更新为2 就往另一个表 change里面插入这条更新后的数据。如果num更新为3就把更新前的数据和更新后的数据都插入到change里面,而且字段num都是3.change 里面也有num字段。
[最优解释]

create trigger t1
on tb
for update
as
begin
    if exists(select 1 from inserted where num=2)
      insert into change 
      select * from change
    else exists(select 1 from inserted where num=3)
      insert into change
      select * from inserted
      union
      select * from deleted --插入式把num列的值改为3
end
       
[其他解释]
create trigger t1 
on tb 
for update
as
begin    
if update (num)
begin
if exists(select 1 from inserted where num=2)       
   insert into change        
   select * from inserted 
   where num = 2     
if exists(select 1 from inserted where num=3)       
   insert into change       
   select * from inserted       
   where num = 3
union all     
   select d.col1,d.col2,num=3,d.col4... from deleted d,inserted i
   where i.key = d.key
   and i.num = 3 
end
end

[其他解释]
引用:
SQL code?123456789101112131415161718192021create trigger t1 on tb for updateasbegin    if update (num)beginif exists(select 1 from inserted where num=2)          insert into change       ……
顶海爷

热点排行
Bad Request.