//增加
CREATE TRIGGER [dc_jhbid_tr] ON [dbo].[dc_jhbid] 
FOR INSERT
AS
update b set  b.dglsl=b.dglsl-i.dsl from dc_htdd b,inserted i
where i.dhtbm=b.dhtbm and i.dcpbm=b.dhtbm
//修改
CREATE TRIGGER [dc_jhbid_tr] ON [dbo].[dc_jhbid] 
FOR UPDATEAS
IF UPDATE (dsl)  -- is update
update b set  b.dglsl=b.dglsl-(d.dsl-i.dsl)
from dc_htdd b,inserted i,deleted d
 where i.dhtbm =b.dhtbm and i.dhtbm=b.dcpbm and d.dhtbm=b.dhtbm  and d.dhtbm =b.dcpbm
//删除
CREATE TRIGGER [dc_jhbid_tr] ON [dbo].[dc_jhbid] 
FOR delete
update b set  b.dglsl=b.dglsl-d.dsl from dc_htdd b,deleted d
 where d.dhtbm=b.dhtbm  and d.dhtbm =b.dcpbm

解决方案 »

  1.   

    你的IF判断是错误的,程序永远不会执行IF块中的处理
      

  2.   

    你的@nInsRows是什么东西啊?没有值?
      

  3.   

    SELECT @nCount = COUNT(*) FROM DELETED
    select @ninsRows=count(*0 from inserted  --add this statement
      

  4.   

    sorry .try below triggerCREATE TRIGGER [dc_jhbid_tr] ON [dbo].[dc_jhbid] 
    FOR INSERT, UPDATE, DELETE 
    AS
    DECLARE @nCount int
    DECLARE @nInsRows int SELECT @nCount = COUNT(*) FROM DELETED
    select @ninsRows=count(*) from inserted  --add this statement IF (@nInsRows > 0 AND @nCount > 0) --is insert
    update b set  b.dglsl=b.dglsl-i.dsl from dc_htdd b,inserted i
    where i.dhtbm=b.dhtbm and i.dcpbm=b.dhtbm
    IF update(dc_jhbid)  -- is update
    update b set  b.dglsl=b.dglsl-(d.dsl-i.dsl)
    from dc_htdd b,inserted i,deleted d
     where i.dhtbm =b.dhtbm and i.dhtbm=b.dcpbm and d.dhtbm=b.dhtbm  and d.dhtbm =b.dcpbm
    IF (@nInsRows = 0 AND @nCount > 0) --is insert
    update b set  b.dglsl=b.dglsl+d.dsl from dc_htdd b,deleted d
    where d.dhtbm=b.dhtbm and d.dcpbm=b.dhtbm
      

  5.   

    这样的触发器,在INSERT 一个新的(dc_htdd 表原来没有的)dhtbm 时,触发器不会起作用,因为你的触发器里只有UPDATE,没有INSERT。
    看看
    http://www.csdn.net/expert/topic/646/646270.xml?temp=.6881983
    也许对你有帮助。