Change this line:
SET @Bad_PackCode = SELECT TOP(1) PackCode FROM inserted --there is error
to this:
SELECT TOP(1) @Bad_PackCode=PackCode FROM inserted
سؤال
Working with SQL Server 2005 Creating Trigger which checks if inserting not already exist. Having problem getting record parameter, there is the code:
CREATE TRIGGER t_MFShiftTypeOperation ON [CAST$MFShiftTypeOperation]
FOR INSERT, UPDATE AS
IF @@ROWCOUNT=1 BEGIN
IF EXISTS (SELECT * FROM inserted AS I
JOIN CAST$MFShiftTypeOperation AS STO
ON ((I.ShiftTypeCode = STO.ShiftTypeCode AND
I.PackCode = STO.PackCode) OR
I.RegisterAppCode = STO.RegisterAppCode))
BEGIN
DECLARE @Bad_PackCode AS EmpUserCode_t
SET @Bad_PackCode = SELECT TOP(1) PackCode FROM inserted --there is error
ROLLBACK TRAN
PRINT 'Оperation with '+ @Bad_PackCode +' already exist'
END
END
when I'm trying to execute code, it throws me error message:
Msg 156, Level 15, State 1, Procedure t_MFShiftTypeOperation, Line 16
Incorrect syntax near the keyword 'SELECT'.
Can someone explain where is mistake, or suggest better solution.
المحلول
Change this line:
SET @Bad_PackCode = SELECT TOP(1) PackCode FROM inserted --there is error
to this:
SELECT TOP(1) @Bad_PackCode=PackCode FROM inserted