读书人

在触发器中 if upfdte()可以判断某张表

发布时间: 2012-01-23 21:57:28 作者: rapoo

在触发器中 if upfdte()可以判断某张表的某一列被更新了吗?
我想判断a(a 表是另外的表,不是我要更改的表哟)表的m列,如果m列的值为0,则执行下面的更改。

[解决办法]

探讨
我想判断a(a 表是另外的表,不是我要更改的表哟)表的m列,如果m列的值为0,则执行下面的更改。

[解决办法]
SQL code
if object_id('a','U') is not null   drop table agocreate table a( id int, m int)goinsert into aselect 1,2 union allselect 2,0 goif object_id('b','u') is not null   drop table bgocreate table b( id int, c varchar(10))goinsert into bselect 1,'11' union allselect 2,'22'goif object_id('tr_b','tr') is not null   drop trigger tr_bgocreate trigger tr_b on bfor updateas  if not exists(select 1 from a inner join inserted on a.id=inserted.id and a.m=0)     rollbackgoupdate b set c='111' where id=1  --A表中id为1的m不等于0,终止更新goupdate b set c='222' where id=2  --A表中id为2的m等于0,继续更新goselect * from b  --可以看到第一条更新未执行,第二条更新执行成功/*id          c----------- ----------1           112           222(2 行受影响)*/ 

读书人网 >SQL Server

热点推荐