我正在尝试在我的雇员表上创建一个触发器,该触发器不应允许插入一个雇用日期早于其老板的雇用日期的新雇员
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中编写触发器来处理INSERTED
可能包含多个记录这一事实。您不能假设它只会是一条记录。我认为以下是您要寻找的内容: