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

Sql server触发器的有关问题

2012-01-28 
Sql server触发器的问题我很菜,别要笑我问题如下有一table:a如下abis_confirmeduid1122N13344N2table:ba1b

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

热点排行
Bad Request.