当前位置: 代码迷 >> Sql Server >> 一个触发器的写法,该如何处理
  详细解决方案

一个触发器的写法,该如何处理

热度:66   发布时间:2016-04-27 15:14:49.0
一个触发器的写法
有一张表table1
id     flag
  1         0
  2         1

如果table1的flag字段改为2时触发

修改另外一张表table2的Rec_stat号为2,通过id来关联
id     Rec_stat
  2           1

------解决方案--------------------
create trigger tu_table1 on table1
for update
as
if update(flag) and exists(select 1 from inserted i,deleted d where i.id=d.id and i.flag=2 and isnull(d.flag,0) <> 2)
begin
update table2
set Rec_start=i.flag
from table2,inserted i,deleted d
where table2.id=i.id and i.id=d.id and i.flag=2 and isnull(d.flag,0) <> 2
end
------解决方案--------------------
create trigger tu_table1 on table1
for update
as
if update(flag) and exists(select 1 from inserted i,deleted d where i.id=d.id and i.flag=2 and isnull(d.flag,0) <> 2)
begin
update table2
set Rec_start=i.flag
from table2,inserted i,deleted d
where table2.bill_no=i.form_id and i.id=d.id and i.flag=2 and isnull(d.flag,0) <> 2
and table2.idate=(select max(a.idate) from table2 a where a.bill_no=table2.bill_no)
end
  相关解决方案