How can I get the actual stored procedure line number from an error message?

前端 未结 9 2389
长情又很酷
长情又很酷 2020-12-04 07:02

When I use SQL Server and there\'s an error, the error message gives a line number that has no correlation to the line numbers in the stored procedure. I assume that the dif

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 07:23

    The long answer: the line number is counted from the CREATE PROCEDURE statement, plus any blank lines or comment lines you may have had above it when you actually ran the CREATE statement, but not counting any lines before a GO statement…

    I found it much easier to make a stored proc to play around with to confirm:

    GO
    
    -- =============================================
    -- Author:          
    -- Create date: 
    -- Description:     
    -- =============================================
    CREATE PROCEDURE ErrorTesting
           -- Add the parameters for the stored procedure here
    AS
    BEGIN
           -- SET NOCOUNT ON added to prevent extra result sets from
           -- interfering with SELECT statements.
           SET NOCOUNT ON;
    
           -- Insert statements for procedure here
           SELECT 1/0
    
    END
    GO
    

    After you’ve created it, you can switch it to ALTER PROCEDURE and add some blank lines above the comments and above and below the first GO statement to see the effect.

    One very strange thing I noticed was that I had to run EXEC ErrorTesting in a new query window instead of highlighting it at the bottom of the same window and running… When I did that the line numbers kept going up! Not sure why that happened..

提交回复
热议问题