日期:2014-05-17  浏览次数:20516 次

请问大家一个触发器中更新语句的问题
请教大家一个问题,我在触发器要执行下面的操作
update t1 set t1.price=t2.price
from table 1,inserted t2
where t1.ID=t2.SourceID
and t2.SourceType='采购订单'

我需不需要在这个语句前面加一个if条件呢?比如:
if exists(select 1 from inserted where SourceType='采购订单')
begin

update t1 set t1.price=t2.price
from table 1,inserted t2
where t1.ID=t2.SourceID
and t2.SourceType='采购订单'

end

我在所有的触发器的第一行都有加
if @@rowcount=0 return

请问,如果不加if条件,会有可能存在什么隐患么?

------解决方案--------------------
Update不需要了吧,没有数据就更新0条而已!
------解决方案--------------------
哦,不用加的,因为你在update语句中加了条件: t2.SourceType='采购订单'

所以,经过这个条件过滤之后,只有满足要求的数据,才会被update
------解决方案--------------------
if @@rowcount=0 return 
这句是表有数据往下执行,表不会删到只有0条加不加都一样

if exists(select top 1 SourceType from inserted where SourceType='采购订单')
这句这样就快很多