SQL Server ERROR_LINE Function: A How-To Guide
In this article
The SQL Server ERROR_LINE() function returns the line number in the batch or stored procedure where an error occurred. It is most useful inside a TRY...CATCH block, where it tells you exactly which line of your T-SQL code triggered the CATCH handler. To use it, wrap your statements in BEGIN TRY...END TRY and BEGIN CATCH...END CATCH, then call ERROR_LINE() in the CATCH block. No special setup is required beyond SQL Server 2005 or later (any supported version works).
What You'll Need
- SQL Server 2005 or later (ERROR_LINE is available in all supported versions).
- A database connection with permission to create and run T-SQL batches (e.g., a login with db_owner or equivalent).
- SQL Server Management Studio (SSMS) or Azure Data Studio, or any client that can execute T-SQL.
- Basic familiarity with TRY...CATCH error handling.
Step-by-Step
- Open a new query window in SSMS or Azure Data Studio and connect to your database. The exact menu labels can differ slightly by version, but you can usually use File > New > Query with Current Connection or press Ctrl+N.
- Write a simple batch with a deliberate error inside a TRY block. For example:
This returns the line number of the SELECT 1/0 statement (line 2 in the batch).BEGIN TRY SELECT 1/0; -- division by zero END TRY BEGIN CATCH SELECT ERROR_LINE() AS ErrorLine; END CATCH - Run the batch (F5 or the Execute button). The result grid shows a single column ErrorLine with the line number where the error occurred.
- Test with a stored procedure to see how line numbers are reported. Create a procedure that contains a TRY...CATCH and an error:
Execute it withCREATE PROCEDURE dbo.TestErrorLine AS BEGIN BEGIN TRY SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_LINE() AS ErrorLine; END CATCH ENDEXEC dbo.TestErrorLine;. The line number returned is relative to the procedure definition, not the outer batch. - Capture the line number for logging by inserting it into an error table along with other ERROR_* functions. For example:
This preserves the line number for later analysis.INSERT INTO dbo.ErrorLog (ErrorNumber, ErrorMessage, ErrorLine) SELECT ERROR_NUMBER(), ERROR_MESSAGE(), ERROR_LINE(); - Understand scope: ERROR_LINE() returns NULL if called outside a CATCH block. It is also NULL if the error did not occur in a line of T-SQL (for example, a hardware error). Always call it inside the CATCH block immediately after the error.
- Use with other ERROR_* functions such as ERROR_MESSAGE(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_PROCEDURE(), and ERROR_NUMBER() to build a complete error record. These functions are only valid inside a CATCH block.
- Test nested TRY...CATCH to see how line numbers propagate. If an inner CATCH re-raises the error with THROW or RAISERROR, the outer CATCH will report the line number of the re-raise statement, not the original error line. Plan your logging accordingly.
Common Mistakes
- Calling ERROR_LINE() outside a CATCH block. It returns NULL, which can be mistaken for a missing line number.
- Assuming the line number is relative to the entire script file. It is relative to the batch or stored procedure definition. If you run multiple batches separated by GO, each batch starts at line 1.
- Forgetting that line numbers can shift when you edit the code. Always log the line number together with the error message and procedure name for context.
- Using ERROR_LINE() after a statement that clears the error context (e.g., after another statement in the CATCH block). Capture it into a variable first if you need it later.
- Expecting ERROR_LINE() to work in a trigger or scalar function in the same way. It does work in triggers, but line numbers are relative to the trigger definition.
Tips
- Store ERROR_LINE() in a local variable at the very start of the CATCH block to avoid losing it if subsequent statements raise new errors.
- Combine ERROR_LINE() with ERROR_PROCEDURE() to pinpoint the exact location in modular code.
- Use THROW instead of RAISERROR when re-throwing errors; THROW preserves the original error information better, but note that ERROR_LINE() in an outer CATCH will reflect the THROW statement's line.
- For detailed debugging, consider using the output of ERROR_LINE() alongside the actual line of code from source control or your script file.
- Remember that line numbers are 1-based and count from the start of the batch or procedure body, including blank lines and comments.
Troubleshooting
- ERROR_LINE() returns NULL: You are likely outside a CATCH block, or the error was not a T-SQL error (e.g., a constraint violation may still return a line). Ensure the call is directly inside the CATCH block.
- Line number seems wrong: Check whether the code is inside a stored procedure, trigger, or batch. Line numbers reset for each module. Also, if you used dynamic SQL (EXEC or sp_executesql), the line number refers to the dynamic batch, not the outer procedure.
- Line number changes after editing: This is expected. Always log the error message and procedure name for context.
- ERROR_LINE() not available: You are using SQL Server 2000 or earlier. Upgrade to a supported version.
- Permission issues: If you cannot create procedures or run batches, ask your DBA for appropriate rights.
Frequently Asked Questions
What does ERROR_LINE() return outside a CATCH block?
It returns NULL. ERROR_LINE() is only valid inside a CATCH block.
Is the line number relative to the entire script or the batch?
It is relative to the batch or the stored procedure/trigger definition. Batches separated by GO start at line 1.
Can I use ERROR_LINE() in a stored procedure?
Yes, inside a TRY...CATCH block within the procedure. The line number is relative to the procedure body.
Does ERROR_LINE() work with dynamic SQL?
Yes, but the line number refers to the dynamic batch executed by sp_executesql or EXEC, not the outer procedure.
What other ERROR_* functions can I use with ERROR_LINE()?
ERROR_MESSAGE(), ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_PROCEDURE(), and ERROR_LINE() itself. All are valid only inside a CATCH block.
Why does ERROR_LINE() return a different number after I add comments?
Line numbers count every line, including comments and blank lines. Adding lines shifts the numbers.