这是我的代码:
CREATE TRIGGER trg_Booking
ON Bookings
FOR INSERT, UPDATE
DECLARE @bef AS varchar(25)
SELECT @bef = Role
FROM Inserted, booksys.dbo.Employees
WHERE Inserted.EmployeeNo = Employees.EmployeeNo
IF NOT (@bef = 'Foreman')
BEGIN
RAISERROR ('Only a Foreman can do that!',16,1)
ROLLBACK TRANSACTION
END
尝试执行并创建触发器时,我不断收到错误消息; “Msg 156,Level 15,State 1,Procedure trg_booking,Line 5 关键字'DECLARE'附近的语法不正确。“
另外,似乎SQL Server无法识别对象“已插入”?
我正在使用SQL Server 2014和Management Studio。
答案 0 :(得分:0)
As
FOR INSERT, UPDATE
如果trigger
内有多个语句,请始终使用Begin and END
CREATE TRIGGER booksys.dbo.trg_Booking
ON booksys.dbo.Bookings
FOR INSERT, UPDATE
as
Begin
IF Not exists ( SELECT 1
FROM Inserted Inner Join booksys.dbo.Employees
on Inserted.EmployeeNo = Employees.EmployeeNo
where Role='Foreman')
BEGIN
RAISERROR ('Only a Foreman can do that!',16,1)
ROLLBACK TRANSACTION
END
END