ERROR_MESSAGE (Transact-SQL)ERROR_MESSAGE (Transact-SQL)

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

此函式會傳回造成執行 TRY...CATCH 建構的 CATCH 區塊之錯誤的訊息文字。This function returns the message text of the error that caused the CATCH block of a TRY...CATCH construct to execute.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

ERROR_MESSAGE ( )   

傳回類型Return Types

nvarchar(4000)nvarchar(4000)

傳回值Return Value

在 CATCH 區塊中呼叫時,ERROR_MESSAGE 會傳回造成執行 CATCH 區塊之錯誤訊息的完整文字。When called in a CATCH block, ERROR_MESSAGE returns the complete text of the error message that caused the CATCH block to run. 文字包括提供給任何可替代參數的值;例如,長度、物件名稱或次數。The text includes the values supplied for any substitutable parameters - for example, lengths, object names, or times.

在 CATCH 區塊範圍之外呼叫時,ERROR_MESSAGE 會傳回 NULL。ERROR_MESSAGE returns NULL when called outside the scope of a CATCH block.

RemarksRemarks

ERROR_MESSAGE 支援在 CATCH 區塊範圍內的任何位置呼叫。ERROR_MESSAGE supports calls anywhere within the scope of a CATCH block.

不論執行多少次,或在 CATCH 區塊範圍內的哪個位置執行,ERROR_MESSAGE 都會傳回相關的錯誤訊息。ERROR_MESSAGE returns a relevant error message regardless of how many times it runs, or where it runs within the scope of the CATCH block. 這有別於 @@ERROR 之類的函式,它們只會在緊接於發生錯誤的陳述式之後的陳述式中,傳回錯誤號碼。This contrasts with a function like @@ERROR, which only returns an error number in the statement immediately following the one that causes an error.

在巢狀 CATCH 區塊中,ERROR_MESSAGE 會參考該 CATCH 區塊之 CATCH 區塊範圍特定的錯誤訊息。In nested CATCH blocks, ERROR_MESSAGE returns the error message specific to the scope of the CATCH block that referenced that CATCH block. 例如,外部 TRY...CATCH 建構的 CATCH 區塊可能會有內部 TRY...CATCH 建構。For example, the CATCH block of an outer TRY...CATCH construct could have an inner TRY...CATCH construct. 在該內部 CATCH 區塊內,ERROR_MESSAGE 會傳回叫用內部 CATCH 區塊之錯誤的訊息。Inside that inner CATCH block, ERROR_MESSAGE returns the message from the error that invoked the inner CATCH block. 如果 ERROR_MESSAGE 是在外部 CATCH 區塊中執行,它會傳回叫用該外部 CATCH 區塊之錯誤的訊息。If ERROR_MESSAGE runs in the outer CATCH block, it returns the message from the error that invoked that outer CATCH block.

範例Examples

A.A. 在 CATCH 區塊中使用 ERROR_MESSAGEUsing ERROR_MESSAGE in a CATCH block

此範例會顯示產生除以零之錯誤的 SELECT 陳述式。This example shows a SELECT statement that generates a divide-by-zero error. CATCH 區塊會傳回錯誤訊息。The CATCH block returns the error message.

BEGIN TRY  
    -- Generate a divide-by-zero error.  
    SELECT 1/0;  
END TRY  
BEGIN CATCH  
    SELECT ERROR_MESSAGE() AS ErrorMessage;  
END CATCH;  
GO  

以下為結果集:Here is the result set.

-----------

(0 row(s) affected)

ErrorMessage
----------------------------------
Divide by zero error encountered.

(1 row(s) affected)

B.B. 在含有其他錯誤處理工具的 CATCH 區塊中使用 ERROR_MESSAGEUsing ERROR_MESSAGE in a CATCH block with other error-handling tools

此範例會顯示產生除以零之錯誤的 SELECT 陳述式。This example shows a SELECT statement that generates a divide-by-zero error. 除了錯誤訊息,CATCH 區塊也會傳回該錯誤的相關資訊。Along with the error message, the CATCH block returns information about that error.

BEGIN TRY  
    -- Generate a divide-by-zero error.  
    SELECT 1/0;  
END TRY  
BEGIN CATCH  
    SELECT  
        ERROR_NUMBER() AS ErrorNumber  
        ,ERROR_SEVERITY() AS ErrorSeverity  
        ,ERROR_STATE() AS ErrorState  
        ,ERROR_PROCEDURE() AS ErrorProcedure  
        ,ERROR_LINE() AS ErrorLine  
        ,ERROR_MESSAGE() AS ErrorMessage;  
END CATCH;  
GO  

以下為結果集:Here is the result set.

-----------

(0 row(s) affected)

ErrorNumber ErrorSeverity ErrorState  ErrorProcedure  ErrorLine  ErrorMessage
----------- ------------- ----------- --------------- ---------- ----------------------------------
8134        16            1           NULL            4          Divide by zero error encountered.

(1 row(s) affected)

另請參閱See Also

sys.messages (Transact-SQL) sys.messages (Transact-SQL)
TRY...CATCH (Transact-SQL) TRY...CATCH (Transact-SQL)
ERROR_LINE (Transact-SQL) ERROR_LINE (Transact-SQL)
ERROR_MESSAGE (Transact-SQL) ERROR_MESSAGE (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL) ERROR_PROCEDURE (Transact-SQL)
ERROR_SEVERITY (Transact-SQL) ERROR_SEVERITY (Transact-SQL)
ERROR_STATE (Transact-SQL) ERROR_STATE (Transact-SQL)
RAISERROR (Transact-SQL) RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL) @@ERROR (Transact-SQL)
錯誤和事件參考 (資料庫引擎)Errors and Events Reference (Database Engine)