使用游标:Cursor;
CREATE TRIGGER ut_AgentPay_D ON AgentPay FOR DELETE AS
BEGIN --BEGIN DELETE
DECLARE @CustNo_D VARCHAR(8) ,@ThisRec_D Money , @Cancel_D BIT , @CheckNo_D VarChar(4)
----------更新 Customer 表 MustRec / HavedRec 字段------------------
DECLARE AgentPay_Cursor_D CURSOR FOR SELECT CustNo , Cancel , CheckNo , ThisRec From Deleted ;
--定义游标
OPEN AgentPay_Cursor_D
--打开游标
FETCH NEXT FROM AgentPay_Cursor_D INTO @CustNo_D , @Cancel_D , @CheckNo_D , @ThisRec_D
--取第一行值
WHILE @@FETCH_STATUS = 0
--是否正确取出值
BEGIN
IF ( @Cancel_D = 0 ) AND ( NOT @CheckNo_D IS NULL )
UPDATE Customer SET MustRec = MustRec + @ThisRec_D WHERE CustNo = @CustNo_D
--删除收款单时应收款应增加
FETCH NEXT FROM AgentPay_Cursor_D INTO @CustNo_D , @Cancel_D , @CheckNo_D , @ThisRec_D
--取游标下一行
END
CLOSE AgentPay_Cursor_D
--关闭游标
DEALLOCATE AgentPay_Cursor_D
--释放资源
END -- END DELETE