[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-08-12
2
Medium Priority
?
1,152 Views
Last Modified: 2014-08-20
Hi,

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.
0
Comment
Question by:quasar_ee
2 Comments
 
LVL 11

Accepted Solution

by:
HuaMinChen earned 750 total points
ID: 40257647
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.
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 750 total points
ID: 40257712
Hi,

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.

HTH
  David

Here are some code snippets:

create procedure dbo.YourProcedureNameHere
	@Results int output
	, @Debug int = 0
as
	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 
as
	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( 
			LogDateTime
			, CallingProcedureName
			, CallingLocationNote
			, error_number
			, error_severity
			, error_state
			, error_procedure
			, error_line
			, error_message
			)
			values(
				getdate()
				, @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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.
Suggested Courses

830 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