Skip to content

Latest commit

 

History

History
157 lines (122 loc) · 6.13 KB

File metadata and controls

157 lines (122 loc) · 6.13 KB

title: "ERROR_PROCEDURE (Transact-SQL) | Microsoft Docs" ms.custom: "" ms.date: "03/16/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database, sql-data-warehouse, pdw" ms.reviewer: "" ms.suite: "sql" ms.technology: t-sql ms.tgt_pltfrm: "" ms.topic: "language-reference" f1_keywords:

  • "ERROR_PROCEDURE_TSQL"
  • "ERROR_PROCEDURE" dev_langs:
  • "TSQL" helpviewer_keywords:
  • "ERROR_PROCEDURE function"
  • "messages [SQL Server], trigger where occurred"
  • "errors [SQL Server], stored procedure where occurred"
  • "TRY...CATCH [SQL Server]"
  • "CATCH block"
  • "messages [SQL Server], stored procedure where occurred"
  • "errors [SQL Server], trigger where occurred" ms.assetid: b81edbf0-856a-498f-ba87-48ff1426d980 caps.latest.revision: 44 author: MashaMSFT ms.author: mathoma manager: craigg monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017"

ERROR_PROCEDURE (Transact-SQL)

[!INCLUDEtsql-appliesto-ss2008-all-md]

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.

Topic link icon Transact-SQL Syntax Conventions

Syntax

ERROR_PROCEDURE ( )  

Return Types

nvarchar(128)

Return Value

When called in a stored procedure CATCH block where an error occurs, ERROR_PROCEDURE returns the name of that stored procedure.

ERROR_PROCEDURE returns NULL if the error did not occur within a stored procedure or trigger.

ERROR_PROCEDURE returns NULL when called outside the scope of a CATCH block.

Remarks

ERROR_PROCEDURE supports calls anywhere within the scope of a CATCH block.

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. This contrasts with a function like @@ERROR, which only returns an error number in the statement immediately following the one that causes an error.

In a nested CATCH block, ERROR_PROCEDURE returns the error number specific to the scope of the CATCH block that referenced that CATCH block. For example, the CATCH block of an outer TRY...CATCH construct could have an inner TRY...CATCH construct. Inside that inner CATCH block, ERROR_PROCEDURE returns the number of the error that invoked the inner CATCH block. If ERROR_PROCEDURE runs in the outer CATCH block, it returns the number of the error that invoked that outer CATCH block.

Examples: [!INCLUDEssSDWfull] and [!INCLUDEssPDW]

A. Using ERROR_PROCEDURE in a CATCH block

This example shows a stored procedure that generates a divide-by-zero error. 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  

-----------

(0 row(s) affected)

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

(1 row(s) affected)

B. 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  

-----------

(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)
TRY...CATCH (Transact-SQL)
ERROR_LINE (Transact-SQL)
ERROR_MESSAGE (Transact-SQL)
ERROR_NUMBER (Transact-SQL)
ERROR_SEVERITY (Transact-SQL)
ERROR_STATE (Transact-SQL)
RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL)