ERROR_PROCEDURE (Transact-SQL)ERROR_PROCEDURE (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel 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.

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

語法Syntax

ERROR_PROCEDURE ( )  

傳回類型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.

RemarksRemarks

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

不論執行多少次,或在 CATCH 區塊範圍內的哪個位置執行,ERROR_PROCEDURE 都會傳回預存程序或觸發程序的名稱。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 SQL 資料倉儲Azure SQL Data Warehouse平行處理資料倉儲Parallel Data WarehouseExamples: Azure SQL 資料倉儲Azure SQL Data Warehouse 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  

-----------

(0 row(s) affected)

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

(1 row(s) affected)

B.B. 在含有其他錯誤處理工具的 CATCH 區塊中使用 ERROR_PROCEDUREUsing 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) 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)