2

我创建了一个简单的删除触发器,该触发器被触发但 0 行受到影响。父表记录已删除。但是表中的记录也应该删除而不是删除。我有以下查询。

ALTER trigger [dbo].[studentAuditActionDelete] on [dbo].[tblStudent]

after delete
AS

declare @studentId_Audit int;

    select @studentId_Audit = i.studentID from inserted i;  
    delete from studentAudit
    where
    studentId_Audit = @studentId_Audit
    PRINT 'AFTER Delete trigger fired.'
4

2 回答 2

4

您是否尝试过从deleted

select @studentId_Audit = d.studentID from deleted d;

请记住,deleted可以包含多行,您只能选择其中之一!(这是为了让所有触发器记住insertedupdateddeleted

于 2013-03-09T07:25:05.497 回答
0

不要在触发器中声明变量,将简单的 JOIN 与源表 DELETED 一起使用,您将能够使用多次删除

CREATE TRIGGER [dbo].[studentAuditActionDelete] on [dbo].[tblStudent]
FOR DELETE
AS
  DELETE a
  FROM studentAudit a JOIN deleted d ON a.studentId_Audit = d.studentID  
  PRINT 'AFTER Delete trigger fired.'  
于 2013-03-09T08:24:55.563 回答