I'm currently struggling in a database management class. I have been tasked with creating a trigger that prints a message if a customer makes a reservation for the first time. My code so far is as follows
ALTER TRIGGER [dbo].[ResNewTrigger]
ON [dbo].[Reservation]
AFTER INSERT, DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @cid CHAR(4)
DECLARE @res_counts INT
SELECT @cid = CustomerNum FROM inserted
SELECT @res_counts = COUNT(*)
FROM dbo.Reservation
WHERE CustomerNum = @cid
IF @res_counts = 0 THEN
PRINT 'A reservation is made for the first time from customer' + @cid;
END IF;
END;
This throws the errors:
Msg 156, Level 15, State 1, Procedure ResNewTrigger, Line 20 [Batch Start Line 7]
Incorrect syntax near the keyword 'THEN'.Msg 102, Level 15, State 1, Procedure ResNewTrigger, Line 22 [Batch Start Line 7]
Incorrect syntax near ';'.
I must not know the proper syntax or something but I have had zero luck finding how to solve this issue.
inserted- you can never do that asinsertedwill contain as many rows as are updated/inserted, and you have to be able to handle that. There are many SQL Server trigger tutorials around which show how.if/begin/enddoesn't start withthennor finish withend if.