2

我有一张桌子Employee

  Emp_Id  Name
  1       XYZ
  2       ABC

和另一张桌子DivisionInfo

  DivisionID  Division  Emp_Id
       1       mmm          1
       2       nnnn         1
       3       oooo         1

Employee如果表Emp_Id中存在,我需要编写一个触发器来回滚表的更新DivisionInfo。在这种情况下Emp_Id = 1

如何获取 SQL 触发器中正在更新的行的行 ID?

这是我的触发代码:

ALTER TRIGGER [dbo].[EmployeeTrigger]
ON [dbo].[Employee]
FOR UPDATE 
AS
   IF((SELECT COUNT([DivisionID ]) 
       FROM [DivisionInfo] AS D 
       INNER JOIN Employee AS E ON D.[Emp_Id] = E.Emp_Id
       WHERE D.[Emp_Id] = E.Emp_Id) > 0)
   BEGIN
      RAISERROR ('Testing', 10, 1); 
      ROLLBACK TRANSACTION
      RETURN
   END

这里永远是真的

4

1 回答 1

1

尝试这个:

ALTER TRIGGER [dbo].[EmployeeTrigger]
ON [dbo].[Employee]
FOR UPDATE 
AS
IF((
    SELECT COUNT([DivisionID ])
    FROM [DivisionInfo] D
    JOIN INSERTED I ON I.[Emp_Id] = D.[Emp_Id])>0)
BEGIN
        RAISERROR ('Testing', 10, 1); 
     ROLLBACK TRANSACTION
     RETURN
END
于 2013-03-25T09:29:23.603 回答