ERROR_SEVERITY (Transact-SQL)ERROR_SEVERITY (Transact-SQL)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel 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 ( )  

备注

若要查看 SQL Server 2014 及更早版本的 Transact-SQL 语法,请参阅早期版本文档To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

返回类型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.

备注Remarks

ERROR_SEVERITY 支持在 CATCH 块作用域内的任意位置调用。ERROR_SEVERITY supports calls anywhere within the scope of a CATCH block..

无论 ERROR_SEVERITY 运行多少次或在 CATCH 块作用域内的任意位置运行,它都将返回错误的错误严重性值。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 Synapse AnalyticsAzure Synapse Analytics并行数据仓库Parallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics 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)