ERROR_LINE (Transact-SQL)ERROR_LINE (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database適用対象:Applies to:
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure 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 構文表記規則
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 ブロックの範囲外で呼び出された場合は、NULLNULL, if called outside the scope of a CATCH block.
解説Remarks
ERROR_LINE
は、CATCH ブロックのスコープ内の任意の場所で呼び出すことができます。A 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_LINE を使用するUsing ERROR_LINE in a CATCH block
このコード例は、0 除算エラーを生成する 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_LINE をストアド プロシージャと一緒に使用するUsing ERROR_LINE in a CATCH block with a stored procedure
この例では、0 除算エラーを生成したストアド プロシージャを示します。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_LINE を他のエラー処理ツールと一緒に使用するUsing ERROR_LINE in a CATCH block with other error-handling tools
このコード例は、0 除算エラーを生成する 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)