How to get sql error in stored procedure

前端 未结 3 2020
醉话见心
醉话见心 2020-12-14 23:10

I\'m using SQL Server 2005. I created a stored procedure which works most of the time, but I found an instance of where it doesn\'t do what I want.

Currently, the c

3条回答
  •  粉色の甜心
    2020-12-14 23:36

    You could use a general Try/Catch and then construct more details about the error within the CATCH section e.g.

    DECLARE @DetailedErrorDesc VARCHAR(MAX)
    BEGIN TRY
    
    --tsql code goes here
    
    END TRY
    BEGIN CATCH
    
    SELECT @DetailedErrorDesc =         
      CAST(ERROR_NUMBER() AS VARCHAR) + ' : '+
      CAST(ERROR_SEVERITY() AS VARCHAR) + ' : ' +
      CAST(ERROR_STATE() AS VARCHAR) + ' : ' +
      ERROR_PROCEDURE() + ' : ' +
      ERROR_MESSAGE() + ' : ' +
      CAST(ERROR_LINE() AS VARCHAR);
    
    --Now you can decide what to do with the detailed error message....return it or log it etc
    
    END CATCH
    

提交回复
热议问题