使用插入后插入触发器不起作用

问题描述 投票:0回答:1

我正在尝试在我的雇员表上创建一个触发器,该触发器不应允许插入一个雇用日期早于其老板的雇用日期的新雇员

CREATE TABLE [dbo].[Employees](
    [EID] [int] IDENTITY(1,1) NOT NULL,
    [Ename] [nvarchar](20) NOT NULL,
    [Gender] [nvarchar](1) NOT NULL,
    [IsMarried] [nvarchar](1) NOT NULL,
    [Birthdate] [date] NOT NULL,
    [HireDate] [date] NOT NULL,
    [Salary] [float] NOT NULL,
    [Notes] [nvarchar](200) NULL,
    [NationalityID] [int] NULL,
    [BossID] [int] NULL,
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
(

这是触发代码:

CREATE TRIGGER [dbo].[Trig_04]
   ON  [dbo].[Employees]
   AFTER INSERT
AS 
BEGIN

    if ((select INSERTED.HireDate from INSERTED where BossID <> EID) < 
        (select Employees.HireDate from Employees where EID in (select Employees.BossID from Employees where BossID <> EID)))

    Rollback

END

它可以正常执行(没有错误),但是不起作用,但是当我在子查询中使用employees表而不是插入的表时,它正常运行。有人对此有答案吗?

sql-server tsql database-trigger
1个回答
1
投票

您必须在SQL Server中编写触发器来处理INSERTED可能包含多个记录这一事实。您不能假设它只会是一条记录。我认为以下是您要寻找的内容:

© www.soinside.com 2019 - 2024. All rights reserved.