| | |
Error in Trigger Using TRY CATCH AND IF ELSE
Please support our MS SQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved
![]() |
•
•
Join Date: Jun 2009
Posts: 67
Reputation:
Solved Threads: 0
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:
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.
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:
sql Syntax (Toggle Plain Text)
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.
Last edited by peter_budo; Jun 28th, 2009 at 4:55 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Try this:
Also -- Please use [code] tags in the future, not <code>
sql Syntax (Toggle Plain Text)
CREATE 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 BEGIN catch raiserror('Books UnAvailable Or Cannot Issue More Than 3 Books',16,-1) ROLLBACK tran END catch END
Also -- Please use [code] tags in the future, not <code>
Last edited by sknake; Jun 28th, 2009 at 3:12 am.
•
•
Join Date: Jun 2009
Posts: 67
Reputation:
Solved Threads: 0
•
•
•
•
Try this:
sql Syntax (Toggle Plain Text)
CREATE 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 BEGIN catch raiserror('Books UnAvailable Or Cannot Issue More Than 3 Books',16,-1) ROLLBACK tran END catch END
Also -- Please use [code] tags in the future, not <code>
Thanks for your reply.
But since I am a beginner I have a doubt with RaisError statement.
Can you tell me why are we writing 16,-1?
What does this indicate?
What can be replaced instead of this?
Sorry I am asking so many doubts but I am learning SQL server on my own and I have found this forum very useful specifically for MS SQL SERVER.
Thanks for your reply
The two parameters are severity and state. The severity of the error dictates how MSSQL will handle the error. See http://doc.ddart.net/mssql/sql70/ra-rz_1.htm
Here is a quote:
I hope this answers your question and I'm glad you find daniweb useful. Please be sure to start a new thread when you have new questions and mark old threads as solved. It gives the solvers here incentive to help!
Here is a quote:
MS SQL Syntax (Toggle Plain Text)
severity IS the user-defined severity level associated WITH this message. Severity levels FROM 0 through 18 can be used BY any user. Severity levels 19 through 25 are used only BY members of the sysadmin fixed server role. FOR severity levels 19 through 25, the WITH LOG OPTION IS required. -------------------------------------------------------------------------------- Caution Severity levels 20 through 25 are considered fatal. IF a fatal severity level IS encountered, the client connection IS terminated after receiving the message, AND the error IS logged IN the error log AND the application log.
I hope this answers your question and I'm glad you find daniweb useful. Please be sure to start a new thread when you have new questions and mark old threads as solved. It gives the solvers here incentive to help!
•
•
Join Date: Jun 2009
Posts: 67
Reputation:
Solved Threads: 0
•
•
•
•
The two parameters are severity and state. The severity of the error dictates how MSSQL will handle the error. See http://doc.ddart.net/mssql/sql70/ra-rz_1.htm
Here is a quote:
MS SQL Syntax (Toggle Plain Text)
severity IS the user-defined severity level associated WITH this message. Severity levels FROM 0 through 18 can be used BY any user. Severity levels 19 through 25 are used only BY members of the sysadmin fixed server role. FOR severity levels 19 through 25, the WITH LOG OPTION IS required. -------------------------------------------------------------------------------- Caution Severity levels 20 through 25 are considered fatal. IF a fatal severity level IS encountered, the client connection IS terminated after receiving the message, AND the error IS logged IN the error log AND the application log.
I hope this answers your question and I'm glad you find daniweb useful. Please be sure to start a new thread when you have new questions and mark old threads as solved. It gives the solvers here incentive to help!
Thanks for your prompt reply!!
![]() |
Similar Threads
- Imaginary Error (PHP bug?) (PHP)
- NullPointerException: null (in sun.misc.Floating Decimal) Error? (Java)
- nullpointerexception error (Java)
- Error In trigger. (MS SQL)
- Error In trigger. (VB.NET)
- write Database error.. (C++)
- Logic Error? (Need Help, Getting Exception Part II lol) (Java)
- Error trapping while reading windows XP registry entries (Python)
- Error Throwing In Calculator Code When Divided By '0' (C#)
Other Threads in the MS SQL Forum
- Previous Thread: SQL Query takes lot of time
- Next Thread: Help required for UPDATE TRIGGER
| Thread Tools | Search this Thread |






