ERROR_STATE (Transact-SQL)ERROR_STATE (Transact-SQL)

適用於: 是SQL Server 否Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server noAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

傳回造成執行 TRY...CATCH 建構的 CATCH 區塊之錯誤的狀態碼。Returns the state number of the error that caused the CATCH block of a TRY...CATCH construct to be run.

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

語法Syntax

ERROR_STATE ( )  

傳回類型Return Types

intint

傳回值Return Value

當它在 CATCH 區塊中被呼叫時,會傳回造成執行 CATCH 區塊之錯誤訊息的狀態碼。When called in a CATCH block, returns the state number of the error message that caused the CATCH block to be run.

如果是在 CATCH 區塊範圍之外呼叫,便傳回 NULL。Returns NULL if called outside the scope of a CATCH block.

RemarksRemarks

MicrosoftMicrosoft SQL ServerSQL Server Database EngineDatabase Engine 程式碼中的多個點都可能會引發某些錯誤訊息。Some error messages can be raised at multiple points in the code for the MicrosoftMicrosoft SQL ServerSQL Server Database EngineDatabase Engine. 例如,在許多不同情況下,都有可能產生 "1105" 錯誤。For example, an "1105" error can be raised for several different conditions. 每個產生錯誤的特定狀況,都會指派唯一的狀態碼。Each specific condition that raises the error assigns a unique state code.

當檢視已知問題的資料庫時,例如 MicrosoftMicrosoft 知識庫,您可以利用狀態碼來判斷所記錄的問題,與您遇到的錯誤是否相同。When viewing databases of known issues, such as the MicrosoftMicrosoft Knowledge Base, you can use the state number to determine if the recorded issue might be the same as the error you have encountered. 例如,如果知識庫文件討論狀態為 2 的 1105 錯誤訊息,而您收到的 1105 錯誤訊息的狀態卻是 3,錯誤原因可能不是文章所報告的原因。For example, if a Knowledge Base article discusses an 1105 error message with a state of 2, and the 1105 error message you received had a state of 3, your error probably had a different cause than the one reported in the article.

SQL ServerSQL Server 支援工程師也可以利用錯誤中的狀態碼,來找出原始程式碼發生錯誤的位置,這也許能提供如何診斷問題的其他想法。A SQL ServerSQL Server support engineer can also use the state code from an error to find the location in the source code where that error is being raised, which may provide additional ideas on how to diagnose the problem.

可以在 CATCH 區塊範圍內的任何位置呼叫 ERROR_STATE。ERROR_STATE may be called anywhere within the scope of a CATCH block.

ERROR_STATE 不論執行多少次,也不論是在 CATCH 區塊範圍內的任何位置執行,都會傳回錯誤狀態。ERROR_STATE returns the error state regardless of how many times it is run, or where it is run within the scope of the CATCH block. 這有別於 @@ERROR 之類的函式,因為其只會在緊接於發生錯誤的陳述式之後的陳述式中,或在 CATCH 區塊的第一個陳述式中傳回錯誤號碼。This is in contrast to functions like @@ERROR, which only returns the error number in the statement immediately after the one that causes an error, or in the first statement of a CATCH block.

在巢狀 CATCH 區塊中,ERROR_STATE 會傳回參考它的 CATCH 區塊範圍特定的錯誤狀態。In nested CATCH blocks, ERROR_STATE returns the error state specific to the scope of the CATCH block in which it is referenced. 例如,外部 TRY...CATCH 建構的 CATCH 區塊可能會有巢狀的 TRY...CATCH 建構。For example, the CATCH block of an outer TRY...CATCH construct could have a nested TRY...CATCH construct. 在巢狀 CATCH 區塊內,ERROR_STATE 會從呼叫巢狀 CATCH 區塊的錯誤傳回狀態。Within the nested CATCH block, ERROR_STATE returns the state from the error that invoked the nested CATCH block. 如果 ERROR_STATE 是在外部 CATCH 區塊內執行,它會從呼叫 CATCH 區塊的錯誤傳回狀態。If ERROR_STATE is run in the outer CATCH block, it returns the state from the error that invoked that CATCH block.

範例Examples

A.A. 在 CATCH 區塊中使用 ERROR_STATEUsing ERROR_STATE in a CATCH block

下列範例顯示將會產生除以零之錯誤的 SELECT 陳述式。The following example shows a SELECT statement that generates a divide-by-zero error. 它會傳回錯誤的狀態。The state of the error is returned.

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

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

下列範例顯示將會產生除以零之錯誤的 SELECT 陳述式。The following example shows a SELECT statement that generates a divide-by-zero error. 錯誤的相關訊息會隨同錯誤狀態一起傳回。Along with the error state, information that relates to the error is returned.

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  

範例:Azure SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse and 平行處理資料倉儲Parallel Data Warehouse

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

下列範例顯示將會產生除以零之錯誤的 SELECT 陳述式。The following example shows a SELECT statement that generates a divide-by-zero error. 錯誤的相關訊息會隨同錯誤狀態一起傳回。Along with the error state, information that relates to the error is returned.

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_MESSAGE() AS ErrorMessage;  
END CATCH;  
GO  

另請參閱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_NUMBER (Transact-SQL) ERROR_NUMBER (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL) ERROR_PROCEDURE (Transact-SQL)
ERROR_SEVERITY (Transact-SQL) ERROR_SEVERITY (Transact-SQL)
RAISERROR (Transact-SQL) RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL) @@ERROR (Transact-SQL)
錯誤和事件參考 (資料庫引擎)Errors and Events Reference (Database Engine)