ERROR_SEVERITY (Transact-SQL)ERROR_SEVERITY (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 severity value of the error where an error occurs, if that error caused the CATCH block of a TRY...CATCH construct to execute.

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

語法Syntax

ERROR_SEVERITY ( )  

傳回類型Return Types

intint

傳回值Return Value

在發生錯誤的 CATCH 區塊中呼叫時,ERROR_SEVERITY 會傳回造成執行 CATCH 區塊之錯誤的嚴重性值。When called in a CATCH block where an error occurs, ERROR_SEVERITY returns the severity value of the error that caused the CATCH block to run.

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

RemarksRemarks

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

不論執行多少次,或在 CATCH 區塊範圍內的哪個位置執行,ERROR_SEVERITY 都會傳回錯誤的錯誤嚴重性值。ERROR_SEVERITY returns the error severity value of an error, 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.

ERROR_SEVERITY 通常會在巢狀 CATCH 區塊中作業。ERROR_SEVERITY typically operates in a nested CATCH block. ERROR_SEVERITY 會傳回參考該 CATCH 區塊之 CATCH 區塊範圍特定的錯誤嚴重性值。ERROR_SEVERITY returns the error severity value 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_SEVERITY 會傳回叫用內部 CATCH 區塊之錯誤的嚴重性值。Inside that inner CATCH block, ERROR_SEVERITY returns the severity value of the error that invoked the inner CATCH block. 如果 ERROR_SEVERITY 是在外部 CATCH 區塊中執行,它會傳回叫用該外部 CATCH 區塊之錯誤的錯誤嚴重性值。If ERROR_SEVERITY runs in the outer CATCH block, it returns the error severity value of the error that invoked that outer CATCH block.

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

A.A. 在 CATCH 區塊中使用 ERROR_SEVERITYUsing ERROR_SEVERITY in a CATCH block

此範例會顯示產生除以零之錯誤的預存程序。This example shows a stored procedure that generates a divide-by-zero error. ERROR_SEVERITY 會傳回該錯誤的嚴重性值。ERROR_SEVERITY returns the severity value of that error.

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

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

-----------

(0 row(s) affected)

ErrorSeverity
-------------
16

(1 row(s) affected)

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

此範例會顯示產生除以零之錯誤的 SELECT 陳述式。This example shows a SELECT statement that generates a divide by zero error. 預存程序會傳回錯誤的相關資訊。The stored procedure returns information about the 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_NUMBER (Transact-SQL) ERROR_NUMBER (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL) ERROR_PROCEDURE (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)