SQL Server 2012/2008: handling errors from nested stored procedures

Posted on 2014-08-12
Last Modified: 2014-08-20

I have stored procedure usp_Main that calls multiple nested sprocs. I wrote 'try catch' in the usp_Main and all I want is to know which nested sproc generated error. Can I accomplish this without adding 'try catch' to every nested sproc?

Thank you in advance.
Question by:quasar_ee
    LVL 10

    Accepted Solution

    You can have try..catch within nested procedure, and try to return relevant error from nested procedure. Then you will know which error it is.
    LVL 35

    Assisted Solution

    by:David Todd

    Note from the following
    I'm logging the error procedure name, but also the name of the procedure where I caught the error. So initially I simply have to code one catch block for the top procedure.  
    By using a standard variable name with the name of the procedure, the catch block can almost be entirely the same from procedure to procedure.
    I also include a note string in case I want to tag the catch location inside a procedure.


    Here are some code snippets:

    create procedure dbo.YourProcedureNameHere
    	@Results int output
    	, @Debug int = 0
    	set nocount on
    	declare @ProcedureName sysname
    	set @ProcedureName = 'dbo.YourProcedureNameHere'
    	begin catch
    		execute dbo.spGen_LogCatchSQLError
    			@CallingProcedureName = @ProcedureName
    			, @CallingLocationNote = '' 
    		-- Dummy line for Template
    		--print ''
    	end catch
    create procedure dbo.spGen_LogCatchSQLError
    	@CallingProcedureName sysname
    	, @CallingLocationNote sysname 
    	set nocount on
    	--Stored procedure code here
    	declare @ProcedureName sysname
    	set @ProcedureName = ''
    	begin try
    		-- Dummy line for Template
    		print ''
    		--Stored procedure code here
    		insert dbo.GenericError( 
    			, CallingProcedureName
    			, CallingLocationNote
    			, error_number
    			, error_severity
    			, error_state
    			, error_procedure
    			, error_line
    			, error_message
    				, @CallingProcedureName
    				, @CallingLocationNote
    				, error_number()
    				, error_severity()
    				, error_state()
    				, error_procedure()
    				, error_line()
    				, error_message()
    	end try
    	begin catch
    		execute ErrorTrapping.dbo.spGen_LogCatchSQLError
    			@CallingProcedureName = @ProcedureName
    			, @CallingLocationNote = '' 
    	end catch

    Open in new window


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Via a live example, show how to shrink a transaction log file down to a reasonable size.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now