Get sql statement which throws exception while executing in try block

  • 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

  • you can use SELECT ERROR_LINE() to get the line the error occurred on.

  • 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/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply