Error in Trigger Using TRY CATCH AND IF ELSE

Thread Solved
Reply

Join Date: Jun 2009
Posts: 67
Reputation: S2009 is an unknown quantity at this point 
Solved Threads: 0
S2009 S2009 is offline Offline
Junior Poster in Training

Error in Trigger Using TRY CATCH AND IF ELSE

 
0
  #1
Jun 28th, 2009
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:
  1. ALTER trigger trg_InsertInBookIssuedDetails on BOOKISSUEDDETAILS
  2. for INSERT
  3. as
  4. BEGIN
  5. DECLARE
  6. @UserName nvarchar(50),
  7. @BookID INT,
  8. @ReturnMessage nvarchar(50)
  9. BEGIN try
  10. BEGIN tran
  11. SELECT @UserName = IssuedTo,@BookID = BookID FROM BOOKISSUEDDETAILS
  12. UPDATE NewUserRegister SET NumberOfBooksIssued = NumberOfBooksIssued+1 WHERE UserName = @UserName
  13. if (SELECT Inventory FROM LIBRARYBOOKDETAILS WHERE BookID = @BookID)>1
  14. BEGIN
  15. UPDATE LIBRARYBOOKDETAILS SET Inventory = Inventory - 1 WHERE BookID= @BookID
  16. COMMIT tran
  17. END
  18. END try
  19. ELSE
  20. BEGIN
  21. raiserror('Books UnAvailable Or Cannot Issue More Than 3 Books',16,-1)
  22. ROLLBACK tran
  23. END
  24. END
  25. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,172
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 565
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Error in Trigger Using TRY CATCH AND IF ELSE

 
0
  #2
Jun 28th, 2009
Try this:
  1. CREATE trigger trg_InsertInBookIssuedDetails on BOOKISSUEDDETAILS
  2. for INSERT
  3. as
  4. BEGIN
  5. DECLARE
  6. @UserName nvarchar(50),
  7. @BookID INT,
  8. @ReturnMessage nvarchar(50)
  9. BEGIN try
  10. BEGIN tran
  11. SELECT @UserName = IssuedTo,@BookID = BookID FROM BOOKISSUEDDETAILS
  12. UPDATE NewUserRegister SET NumberOfBooksIssued = NumberOfBooksIssued+1 WHERE UserName = @UserName
  13. if (SELECT Inventory FROM LIBRARYBOOKDETAILS WHERE BookID = @BookID)>1
  14. BEGIN
  15. UPDATE LIBRARYBOOKDETAILS SET Inventory = Inventory - 1 WHERE BookID= @BookID
  16. COMMIT tran
  17. END
  18. END try
  19. BEGIN catch
  20. raiserror('Books UnAvailable Or Cannot Issue More Than 3 Books',16,-1)
  21. ROLLBACK tran
  22. END catch
  23. END

Also -- Please use [code] tags in the future, not <code>
Last edited by sknake; Jun 28th, 2009 at 3:12 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 67
Reputation: S2009 is an unknown quantity at this point 
Solved Threads: 0
S2009 S2009 is offline Offline
Junior Poster in Training

Re: Error in Trigger Using TRY CATCH AND IF ELSE

 
0
  #3
Jun 30th, 2009
Originally Posted by sknake View Post
Try this:
  1. CREATE trigger trg_InsertInBookIssuedDetails on BOOKISSUEDDETAILS
  2. for INSERT
  3. as
  4. BEGIN
  5. DECLARE
  6. @UserName nvarchar(50),
  7. @BookID INT,
  8. @ReturnMessage nvarchar(50)
  9. BEGIN try
  10. BEGIN tran
  11. SELECT @UserName = IssuedTo,@BookID = BookID FROM BOOKISSUEDDETAILS
  12. UPDATE NewUserRegister SET NumberOfBooksIssued = NumberOfBooksIssued+1 WHERE UserName = @UserName
  13. if (SELECT Inventory FROM LIBRARYBOOKDETAILS WHERE BookID = @BookID)>1
  14. BEGIN
  15. UPDATE LIBRARYBOOKDETAILS SET Inventory = Inventory - 1 WHERE BookID= @BookID
  16. COMMIT tran
  17. END
  18. END try
  19. BEGIN catch
  20. raiserror('Books UnAvailable Or Cannot Issue More Than 3 Books',16,-1)
  21. ROLLBACK tran
  22. END catch
  23. 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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,172
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 565
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Error in Trigger Using TRY CATCH AND IF ELSE

 
0
  #4
Jun 30th, 2009
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:
  1. severity
  2. 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.
  3. --------------------------------------------------------------------------------
  4.  
  5. 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!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 67
Reputation: S2009 is an unknown quantity at this point 
Solved Threads: 0
S2009 S2009 is offline Offline
Junior Poster in Training

Re: Error in Trigger Using TRY CATCH AND IF ELSE

 
0
  #5
Jun 30th, 2009
Originally Posted by sknake View Post
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:
  1. severity
  2. 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.
  3. --------------------------------------------------------------------------------
  4.  
  5. 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!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC