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
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 28, 2014 6:42 AM
Points: 24, Visits: 129
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
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 8:41 AM
Points: 616, Visits: 3,581
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: Monday, December 1, 2014 7:30 AM
Points: 2,128, Visits: 5,565
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