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: Friday, March 28, 2014 4:15 AM
Points: 20, Visits: 110
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
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 9:33 AM
Points: 551, Visits: 3,160
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: Today @ 5:39 AM
Points: 2,104, Visits: 5,378
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