ERROR_NUMBER (Transact-SQL)ERROR_NUMBER (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 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 ( )  

备注

若要查看 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_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.

备注Remarks

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

无论 ERROR_NUMBER 运行多少次或在 CATCH 块作用域内的任意位置运行,它都将返回相关的错误号。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)