创建触发器; DECLARE 附近的语法不正确
CREATE TRIGGER; Incorrect syntax near DECLARE
这是我的代码:
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
我在尝试执行和创建触发器时不断收到错误消息; “消息 156,级别 15,状态 1,过程 trg_booking,第 5 行
关键字 'DECLARE'."
附近的语法不正确
此外,SQL服务器似乎无法识别对象"Inserted"?
我正在使用 SQL Server 2014 和 Management Studio。
您在 FOR INSERT, UPDATE
之后错过了 As
如果您在 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
这是我的代码:
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
我在尝试执行和创建触发器时不断收到错误消息; “消息 156,级别 15,状态 1,过程 trg_booking,第 5 行 关键字 'DECLARE'."
附近的语法不正确此外,SQL服务器似乎无法识别对象"Inserted"?
我正在使用 SQL Server 2014 和 Management Studio。
您在 FOR INSERT, UPDATE
As
如果您在 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