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

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

2012-02-07 
一个触发器的写法有一张表table1idflag1021如果table1的flag字段改为2时触发修改另外一张表table2的Rec_st

一个触发器的写法
有一张表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

热点排行