SQL Server Error Line number

Source: Internet
Author: User

Https://msdn.microsoft.com/zh-cn/library/ms178600.aspx

Returns the line number where the error occurred, causing the run TRY ... Catch block for the catch construct.

GrammarError_line () return value

When called in a CATCH block:

    • Returns the line number where the error occurred.

    • If the error occurs in a stored procedure or trigger, the line number in the routine is returned.

If called outside the scope of the CATCH block, NULL is returned.

Example

A. Using Error_line in a CATCH block

The following code example shows a SELECT statement that generates a 0 exception error . returns the line number where the error occurred.

1. BEGIN TRY2.    --Generate a divide-by-zero error.3.    SELECT 1/0; END trybegin CATCH    SELECT error_line () as ErrorLine; END CATCH; GO

The empty line in front of the begin try is also counted, as shown in the following test:

B. With stored procedures use Error_line in CATCH blocks

The following code example shows a stored procedure that generates a 0 exception error. Error_line Returns the line number in the stored procedure 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_exampleprocas    SELECT 1/0; Gobegin try    -Execute the stored procedure inside the try block.    EXECUTE Usp_exampleproc; END trybegin CATCH    SELECT error_line () as ErrorLine; END CATCH; GO

The result shows that the line number is calculated from the go

C. Using Error_line in CATCH blocks with other error handling tools

The following code example shows a SELECT statement that generates a 0 exception error . the information about the error is returned with the line number where the error occurred.

BEGIN TRY    --Generate a divide-by-zero error.    SELECT 1/0; END trybegin 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

Results

Error_line is a great help when we debug the location query error.

SQL Server Error Line number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.