Hi all,
I am creating a trigger which should be triggered on insert to BOOKISSUEDETAILS table. I want to alter the table LIBRARYBOOKDETAILS table only if the Inventory column value is greater than 1.
Initially I have created the trigger in the following way. But now I want to alter the Trigger to include the condition(Inventory > 1) and use the Try catch block to raise error.
I used the following code snippet to alter the trigger, but it is generating the error:
ALTER trigger trg_InsertInBookIssuedDetails on BOOKISSUEDDETAILS
for INSERT
as
BEGIN
DECLARE
@UserName nvarchar(50),
@BookID INT,
@ReturnMessage nvarchar(50)
BEGIN try
BEGIN tran
SELECT @UserName = IssuedTo,@BookID = BookID FROM BOOKISSUEDDETAILS
UPDATE NewUserRegister SET NumberOfBooksIssued = NumberOfBooksIssued+1 WHERE UserName = @UserName
if (SELECT Inventory FROM LIBRARYBOOKDETAILS WHERE BookID = @BookID)>1
BEGIN
UPDATE LIBRARYBOOKDETAILS SET Inventory = Inventory - 1 WHERE BookID= @BookID
COMMIT tran
END
END try
ELSE
BEGIN
raiserror('Books UnAvailable Or Cannot Issue More Than 3 Books',16,-1)
ROLLBACK tran
END
END
END
Error Message:
Msg 156, Level 15, State 1, Procedure trg_InsertInBookIssuedDetails, Line 19
Incorrect syntax near the keyword 'else'.
Msg 156, Level 15, State 1, Procedure trg_InsertInBookIssuedDetails, Line 25
Incorrect syntax near the keyword 'end'.
Can anyone let me know the error? and the changes I should make to perform the desired task with condition.
Please help me out in identifying the error.
Thanks in advance.