日期:2014-05-18  浏览次数:20454 次

Sql server触发器的问题
我很菜,别要笑我
问题如下
有一table:a如下
a         b       is_confirmed       uid
11       22                 N                   1
33       44                 N                   2
table:   b
a1       b1       uid

现在我要的是当a表的   is_confirmed   update为Y的时候我要将update为Y的那笔资料insert到   b表   ,a对应a1,b对应b1,uid为自动递增,不用管!
Create   TRIGGER   xxx   for   update  
as
begin
???

end

谢谢

------解决方案--------------------
Create TRIGGER xxx on a
for update
as
begin
if update(is_confirmed) and exists(select 1 from inserted i,deleted d where i.a=d.a and i.b=d.b and i.is_confirmed= 'Y ' and d.is_confirmed= 'N ')
insert into b(a1,b1)
select a,b from inserted i,deleted d
where i.a=d.a and i.b=d.b and i.is_confirmed= 'Y ' and d.is_confirmed= 'N '
end
------解决方案--------------------
看样子,B表好像事 拿来作A表 的 操作记录的 ,应该不用考虑重复
Create TRIGGER tr_up on [Table a]
for update
as
IF UPDATE (is_confirmed)
begin
insert into [Table b](a1,b1) select a,b from [Table a] where is_confirmed= 'Y '
end
------解决方案--------------------
不重复的话就好说了, 不用去搜索[Table a]直接从inserted取
Create TRIGGER xxx on a for update as
begin
insert into b(a1,b1) select a,b from inserted i,deleted d
where i.a=d.a and i.b=d.b and i.is_confirmed= 'Y ' and d.is_confirmed= 'N '
end