ERROR_LINE (Transact-SQL)ERROR_LINE (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 否Azure SQL 資料倉儲 否平行處理資料倉儲 APPLIES TO: yesSQL Server yesAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

此函式會傳回發生錯誤造成執行 TRY...CATCH 建構之 CATCH 區塊的行號。This function returns the line number of occurrence of an error that caused the CATCH block of a TRY...CATCH construct to execute.

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

語法Syntax

ERROR_LINE ( )  

傳回類型Return Type

intint

傳回值Return Value

在 CATCH 區塊中呼叫時,ERROR_LINE 會傳回When called in a CATCH block, ERROR_LINE returns

  • 發生錯誤的行號the line number where the error occurred
  • 常式中的行號 (如果在預存程序或觸發程序內發生錯誤)the line number in a routine, if the error occurred within a stored procedure or trigger
  • NULL (如果在 CATCH 區塊範圍之外呼叫)。NULL, if called outside the scope of a CATCH block.

RemarksRemarks

可以在 CATCH 區塊範圍內的任何位置呼叫 ERROR_LINEA call to ERROR_LINE can happen anywhere within the scope of a CATCH block.

ERROR_LINE 會傳回發生錯誤的行號。ERROR_LINE returns the line number at which the error occurred. 不論在 CATCH 區塊範圍內的哪個位置呼叫 ERROR_LINE,以及呼叫 ERROR_LINE 的次數為何,都是如此。This happens regardless of the location of the ERROR_LINE call within the scope of the CATCH block, and regardless of the number of calls to ERROR_LINE. 這有別於 @ERROR 之類的函式。This contrasts with functions, such as @@ERROR. @ERROR 會在緊接於發生錯誤的陳述式之後的陳述式中,或在 CATCH 區塊的第一個陳述式中,傳回錯誤號碼。@@ERROR returns an error number in the statement immediately following the one that causes an error, or in the first statement of a CATCH block.

在巢狀 CATCH 區塊中,ERROR_LINE 會傳回參考它的 CATCH 區塊範圍特定的錯誤行號。In nested CATCH blocks, ERROR_LINE returns the error line number specific to the scope of the CATCH block in which it is referenced. 例如,TRY...CATCH 建構的 CATCH 區塊可能包含巢狀的 TRY...CATCH 建構。For example, the CATCH block of a TRY...CATCH construct could contain a nested TRY...CATCH construct. 在巢狀 CATCH 區塊內,ERROR_LINE 會傳回叫用巢狀 CATCH 區塊之錯誤的行號。Within the nested CATCH block, ERROR_LINE returns the line number for the error that invoked the nested CATCH block. 如果 ERROR_LINE 是在外部 CATCH 區塊內執行,它會傳回叫用該特定 CATCH 區塊之錯誤的行號。If ERROR_LINE runs in the outer CATCH block, it returns the line number for the error that invoked that specific CATCH block.

範例Examples

A.A. 在 CATCH 區塊中使用 ERROR_LINEUsing ERROR_LINE in a CATCH block

此程式碼範例會顯示產生除以零之錯誤的 SELECT 陳述式。This code example shows a SELECT statement that generates a divide-by-zero error. ERROR_LINE 會傳回發生錯誤的行號。ERROR_LINE returns the line number where the error occurred.

BEGIN TRY  
    -- Generate a divide-by-zero error.  
    SELECT 1/0;  
END TRY  
BEGIN CATCH  
    SELECT ERROR_LINE() AS ErrorLine;  
END CATCH;  
GO  

以下為結果集:Here is the result set.

Result 
-----------

(0 row(s) affected)

ErrorLine
-----------
4

(1 row(s) affected)

B.B. 在含有預存程序的 CATCH 區塊中使用 ERROR_LINEUsing ERROR_LINE in a CATCH block with a stored procedure

此範例會顯示產生除以零之錯誤的預存程序。This example shows a stored procedure that generates a divide-by-zero error. ERROR_LINE 會傳回發生錯誤的行號。ERROR_LINE returns the line number 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_LINE() AS ErrorLine;  
END CATCH;  
GO  

以下為結果集:Here is the result set.

-----------

(0 row(s) affected)

ErrorLine
-----------
7

(1 row(s) affected)  
   

C.C. 在含有其他錯誤處理工具的 CATCH 區塊中使用 ERROR_LINEUsing ERROR_LINE in a CATCH block with other error-handling tools

此程式碼範例會顯示產生除以零之錯誤的 SELECT 陳述式。This code example shows a SELECT statement that generates a divide-by-zero error. ERROR_LINE 會傳回發生錯誤的行號,以及與錯誤本身相關的資訊。ERROR_LINE returns the line number where the error occurred, and information relating to the error itself.

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           3         Divide by zero error encountered.

(1 row(s) affected)
  

另請參閱See Also

TRY...CATCH (Transact-SQL) TRY...CATCH (Transact-SQL)
sys.messages (Transact-SQL) sys.messages (Transact-SQL)
ERROR_NUMBER (Transact-SQL) ERROR_NUMBER (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)