Events
Apr 8, 3 PM - May 28, 7 AM
Sharpen your AI skills and enter the sweepstakes to win a free Certification exam
Register now!This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
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 syntax conventions
ERROR_LINE ( )
int
When called in a CATCH block, ERROR_LINE
returns
A call to ERROR_LINE
can happen anywhere within the scope of a CATCH block.
ERROR_LINE
returns the line number at which the error occurred. 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
. This contrasts with functions, such as @@ERROR. @@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.
In nested CATCH blocks, ERROR_LINE
returns the error line number specific to the scope of the CATCH block in which it is referenced. For example, the CATCH block of a TRY...CATCH construct could contain a nested TRY...CATCH construct. Within the nested CATCH block, ERROR_LINE
returns the line number for the error that invoked the nested CATCH block. If ERROR_LINE
runs in the outer CATCH block, it returns the line number for the error that invoked that specific CATCH block.
This code example shows a SELECT
statement that generates a divide-by-zero error. 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
This example shows a stored procedure that generates a divide-by-zero error. 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
This code example shows a SELECT
statement that generates a divide-by-zero error. 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
TRY...CATCH (Transact-SQL)
sys.messages (Transact-SQL)
ERROR_NUMBER (Transact-SQL)
ERROR_MESSAGE (Transact-SQL)
ERROR_PROCEDURE (Transact-SQL)
ERROR_SEVERITY (Transact-SQL)
ERROR_STATE (Transact-SQL)
RAISERROR (Transact-SQL)
@@ERROR (Transact-SQL)
Events
Apr 8, 3 PM - May 28, 7 AM
Sharpen your AI skills and enter the sweepstakes to win a free Certification exam
Register now!