Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 ERROR HANDLING

Author  Topic 

mksdf
Starting Member

26 Posts

Posted - 2015-02-24 : 12:13:14
Hi,
I have a load of stored procedures that I'm executing from one stored proc.

I start my stored proc like this


DECLARE @TransactionCountOnEntry INT
DECLARE @ErrorCode INT

SELECT @ErrorCode = @@ERROR

IF @ErrorCode = 0
BEGIN

SELECT @TransactionCountOnEntry = @@TRANCOUNT

-- Set BEGIN TRAN (transition) so can roll back if there is a problem
BEGIN TRANSACTION


I then execute each stored proc in this way


DECLARE @SQL NVARCHAR(MAX)

SET @SQL =
N'
EXECUTE Database.dbo.usp_Setup ''' + @variable
'
EXECUTE sp_executeSQL @SQL


So there are multiple instances of these each with someting slightly different in them.

Then finally I finish up with this

IF @@TRANCOUNT > @TransactionCountOnEntry
BEGIN
IF @ErrorCode = 0
COMMIT TRANSACTION
ELSE
ROLLBACK TRANSACTION
END


Each stored proc being executed does not contain any error handling. Do the individual stored procedures require error handling in them? Does this method work with the current set up? and how would I return which stored proc errored?

Kind regards

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-25 : 04:17:23
1. wrap code that may fail in try/catch blocks. when an error occurs the catch block has access to the error_procedure() function.
2. put error handling in each proc.
Go to Top of Page

mksdf
Starting Member

26 Posts

Posted - 2015-02-25 : 04:19:45
thanks. I'll give it a go
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-02-25 : 07:09:24
Most of the time I can get way with something like the following for exception handling:


SET ANSI_NULLS, QUOTED_IDENTIFIER ON
GO
CREATE TABLE dbo.Exceptions
(
ExceptionId int IDENTITY NOT NULL
CONSTRAINT PK_Exceptions PRIMARY KEY
,ExceptionDate datetime NOT NULL
,ErrorNumber int NOT NULL
,ErrorSeverity int NOT NULL
,ErrorState int NOT NULL
,ErrorProcedure nvarchar(126) NOT NULL
,ErrorLine int NOT NULL
,ErrorMessage nvarchar(2048) NOT NULL
)
GO
CREATE PROCEDURE dbo.ExceptionHandler
AS

SET NOCOUNT ON;

DECLARE @ErrorNumber int = ERROR_NUMBER()
,@ErrorSeverity int = ERROR_SEVERITY()
,@ErrorState int = ERROR_STATE()
,@ErrorProcedure nvarchar(126) = ERROR_PROCEDURE()
,@ErrorLine int = ERROR_LINE()
,@ErrorMessage nvarchar(2048) = ERROR_MESSAGE();

IF @@TRANCOUNT > 0
ROLLBACK;

-- Email alert
DECLARE @mailbody nvarchar(4000) =
'Error ' + CAST(@ErrorNumber AS varchar(20)) + ' occurred in procedure '
+ COALESCE(@ErrorProcedure, 'NA') + ' at line ' + CAST(@ErrorLine AS varchar(20))
+ ' with a severity of ' + CAST(@ErrorSeverity AS varchar(20))
+ ' and a state of ' + CAST(@ErrorState AS varchar(20))
+ '.' + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
+ @ErrorMessage;

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'YourProfile'
,@recipients = 'YourEMail'
,@importance = 'High'
,@subject = '<SystemName> Exception'
,@body = @mailbody;

-- write to log
INSERT INTO dbo.Exceptions
(
ExceptionDate, ErrorNumber, ErrorSeverity, ErrorState
,ErrorProcedure, ErrorLine, ErrorMessage
)
SELECT CURRENT_TIMESTAMP, @ErrorNumber, @ErrorSeverity, @ErrorState
,COALESCE(@ErrorProcedure, N'NA'), @ErrorLine, COALESCE(@ErrorMessage, N'No Message');

RAISERROR
(
N'Error %d occurred in procedure %s at line %d. %s'
,@ErrorSeverity
,@ErrorState
,@ErrorNumber
,@ErrorProcedure
,@ErrorLine
,@ErrorMessage
);
GO
CREATE PROCEDURE dbo.YourMainProc
AS
SET NOCOUNT ON;

BEGIN TRY

-- Do your work here

-- eg 1 Call other procs that do not have error handling
EXEC YourSubProc1;

-- eg 2 An explicit transaction
BEGIN TRAN
INSERT ....
UPDATE ...
etc
COMMIT

-- eg 3 SELECTs
SELECT ...

END TRY
BEGIN CATCH;
EXEC dbo.ExceptionHandler;
END CATCH;
Go to Top of Page
   

- Advertisement -