ERROR_PROCEDURE (Transact-SQL)ERROR_PROCEDURE (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 name of the stored procedure or trigger where an error occurs, if that error caused the CATCH block of a TRY...CATCH construct to execute.

  • SQL Server 2017 到当前版本返回 schema_name.stored_procedure_nameSQL Server 2017 thru current version returns schema_name.stored_procedure_name
  • SQL Server 2016 返回 stored_procedure_nameSQL Server 2016 returns stored_procedure_name

主题链接图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

ERROR_PROCEDURE ( )  

备注

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

返回类型Return Types

nvarchar(128)nvarchar(128)

返回值Return Value

在 CATCH 块中调用时,ERROR_PROCEDURE 返回导致错误的存储过程或触发器的名称。When called in a CATCH block, ERROR_PROCEDURE returns the name of the stored procedure or trigger in which the error originated.

如果存储过程或触发器中未出现该错误,ERROR_PROCEDURE 返回 NULL。ERROR_PROCEDURE returns NULL if the error did not occur within a stored procedure or trigger.

在 CATCH 块作用域外调用时,ERROR_PROCEDURE 返回 NULL。ERROR_PROCEDURE returns NULL when called outside the scope of a CATCH block.

备注Remarks

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

无论 ERROR_PROCEDURE 运行多少次或在 CATCH 块作用域内的任意位置运行,它都将返回出现错误的存储过程或触发器的名称。ERROR_PROCEDURE returns the name of the stored procedure or trigger where an error occurs, 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.

示例:Azure Synapse AnalyticsAzure Synapse Analytics并行数据仓库Parallel Data WarehouseExamples: Azure Synapse AnalyticsAzure Synapse Analytics and 并行数据仓库Parallel Data Warehouse

A.A. 在 CATCH 块中使用 ERROR_PROCEDUREUsing ERROR_PROCEDURE in a CATCH block

下面的示例显示生成被零除错误的存储过程。This example shows a stored procedure that generates a divide-by-zero error. ERROR_PROCEDURE 返回出现错误的存储过程的名称。ERROR_PROCEDURE returns the name of the stored procedure where the error occurred.

-- Verify that the stored procedure does not already exist.  
IF OBJECT_ID ( 'usp_ExampleProc', 'P' ) IS NOT NULL   
    DROP PROCEDURE usp_ExampleProc;  
GO  
  
-- Create a stored procedure that   
-- generates a divide-by-zero error.  
CREATE PROCEDURE usp_ExampleProc  
AS  
    SELECT 1/0;  
GO  
  
BEGIN TRY  
    -- Execute the stored procedure inside the TRY block.  
    EXECUTE usp_ExampleProc;  
END TRY  
BEGIN CATCH  
    SELECT ERROR_PROCEDURE() AS ErrorProcedure;  
END CATCH;  
GO  

下面是结果集:Here is the result set.

-----------

(0 row(s) affected)

ErrorProcedure
--------------------
usp_ExampleProc

(1 row(s) affected)

B.B. 通过其他错误处理工具在 CATCH 块中使用 ERROR_PROCEDURE。Using ERROR_PROCEDURE in a CATCH block with other error-handling tools

下面的示例显示生成被零除错误的存储过程。This example shows a stored procedure that generates a divide-by-zero error. 除了返回出现错误的存储过程的名称外,存储过程将返回有关此错误的信息。Along with the name of the stored procedure where the error occurred, the stored procedure returns information about the error.

-- Verify that the stored procedure does not already exist.  
IF OBJECT_ID ( 'usp_ExampleProc', 'P' ) IS NOT NULL   
    DROP PROCEDURE usp_ExampleProc;  
GO  
  
-- Create a stored procedure that   
-- generates a divide-by-zero error.  
CREATE PROCEDURE usp_ExampleProc  
AS  
    SELECT 1/0;  
GO  
  
BEGIN TRY  
    -- Execute the stored procedure inside the TRY block.  
    EXECUTE usp_ExampleProc;  
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,  
        ERROR_LINE() AS ErrorLine;  
        END CATCH;  
GO

下面是结果集:Here is the result set.

-----------

(0 row(s) affected)

ErrorNumber ErrorSeverity ErrorState  ErrorProcedure   ErrorMessage                       ErrorLine
----------- ------------- ----------- ---------------- ---------------------------------- -----------
8134        16            1           usp_ExampleProc  Divide by zero error encountered.  6

(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_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)