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

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database

此函数返回出现错误的行号,该错误导致执行了 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 ( )

备注

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

返回类型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
  • 如果在 CATCH 块作用域外调用,则返回 NULL。NULL, if called outside the scope of a CATCH block.

备注Remarks

可在 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  

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  

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  

另请参阅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)