ERROR_NUMBER (Transact-SQL)ERROR_NUMBER (Transact-SQL)

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

此函式會傳回造成執行 TRY...CATCH 建構的 CATCH 區塊之錯誤的錯誤號碼。This function returns the error number 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_NUMBER ( )  

傳回類型Return Types

intint

傳回值Return Value

在 CATCH 區塊中呼叫時,ERROR_NUMBER 會傳回造成執行 CATCH 區塊之錯誤的錯誤號碼。When called in a CATCH block, ERROR_NUMBER returns the error number of the error that caused the CATCH block to run.

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

RemarksRemarks

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

不論執行多少次,或在 CATCH 區塊範圍內的哪個位置執行,ERROR_NUMBER 都會傳回相關的錯誤號碼。ERROR_NUMBER returns a relevant error number 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_NUMBER 會參考該 CATCH 區塊之 CATCH 區塊範圍特定的錯誤號碼。In a nested CATCH block, ERROR_NUMBER returns the error number 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_NUMBER 會傳回叫用內部 CATCH 區塊之錯誤的號碼。Inside that inner CATCH block, ERROR_NUMBER returns the number of the error that invoked the inner CATCH block. 如果 ERROR_NUMBER 是在外部 CATCH 區塊中執行,它會傳回叫用該外部 CATCH 區塊之錯誤的號碼。If ERROR_NUMBER runs in the outer CATCH block, it returns the number of the error that invoked that outer CATCH block.

範例Examples

A.A. 在 CATCH 區塊中使用 ERROR_NUMBERUsing ERROR_NUMBER 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 number.

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

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

-----------

(0 row(s) affected)

ErrorNumber
-----------
8134

(1 row(s) affected)

B.B. 在含有其他錯誤處理工具的 CATCH 區塊中使用 ERROR_NUMBERUsing ERROR_NUMBER 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 number, 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)