Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Get sql statement which throws exception while executing in try block Expand / Collapse
Author
Message
Posted Monday, September 24, 2012 3:39 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, June 3, 2015 1:20 AM
Points: 26, Visits: 168
Hi,

I need to get sql statement, which throws an exception while executing in try block.
E.g.
Begin Try
SELECT 1/0
End Try
Begin Catch
Select ERROR_MESSAGE()
End Catch

Result should like this-

Error Message: Divide by zero error encountered.
SQL Statement: SELECT 1/0

Can anybody help me out to get this result?


Thanks & Regards,
Kumar Anand
Post #1363383
Posted Monday, September 24, 2012 3:45 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 10:06 AM
Points: 702, Visits: 4,162
you can use SELECT ERROR_LINE() to get the line the error occurred on.
Post #1363388
Posted Monday, September 24, 2012 3:47 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 25, 2015 7:56 AM
Points: 2,153, Visits: 5,742
I assume that you are using a log table and in the catch block you want to log the information. In that case you can also use the error_line() and error_procedure() function to get the procedure's name and at what line the error occurred.

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1363390
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse