1

I have been researching the TRY/CATCH block and I am a little stumped on how to pass an error the way I need to do so. From what I have read and if I understand correctly, a RAISERROR in a CATCH block in SQL will be passed to the calling batch OR the calling application. The application is running a stored procedure which has a transaction in it. The transaction is wrapped in a TRY/CATCH block. In the CATCH block, I am raising the error if something in the transaction fails causing it to jump to CATCH. If running the procedure via SSMS, the error shows up fine and the transaction rolls back. However, if the application calls the stored procedure and the same error occurs, the application never knows about the error and thus doesn't now the procedure failed.

First of all, am I understanding correctly how the RAISERROR in CATCH works? If so, how can I get that error raised back to the calling application?

BEGIN TRY 
    BEGIN TRAN 
        ............... 
    COMMIT 
END TRY 
BEGIN CATCH 
    IF @@TRANCOUNT > 0 
        ROLLBACK 
    DECLARE @ErrMsg NVARCHAR(4000) 
    SELECT @ErrMsg = ERROR_MESSAGE() 
    RAISERROR(@ErrMsg, 16, 1) 
END CATCH 

I am Running Windows 7, SQL Server 2005

4

1 に答える 1

1

あなたがやろうとしていることを行う最も簡単な方法は、 @ErrMsg をプロシージャの出力パラメーターとして宣言し、呼び出し元のアプリケーションでそれを処理することです。

エラー出力を処理するために RAISEERROR を引き続き使用したい場合は、ここで RAISEERROR が出力を処理する方法を読むことができます。http://msdn.microsoft.com/en-us/library/ms178592.aspx

于 2012-09-21T04:27:38.357 に答える