Solved

Best Practices for Error Handling in Triggers

Posted on 2004-08-10
3
724 Views
Last Modified: 2008-02-01
I have a simple delete trigger in SQL Server 2000 that inserts the deleted record from tblName to a deleted table tblDeletedName. The code isssentially

INSERT INTO tblDeletedName
   (co11,...,coln)
SELECT col1,...coln
FROM deleted

From a best practices perspective should I include an error test and explicitly issue a ROLLBACK TRANSACTION? Should I also issue a RAISERROR and if so does it go before or after the rollback. If I do not of these and an error occurs, will the trigger stop at the point of error and rollback the transaction?

At this time I'm using the following before the insert

DECLARE @ErrNbr INT
DECLARE @ErrMsg VARCHAR(250)

and the following after the insert

SET @ErrNbr = @@ERROR
IF @ErrNbr <> 0 BEGIN
   SET @ErrMsg = 'tblName delete trigger failed on error ' + CAST(@ErrNbr AS VARCHAR(20))
   RAISERROR (@ErrMsg,16,1)
   ROLLBACK TRANSACTION
   RETURN
END

The actual delete command for tblName comes from an ADO command in VBA in an Access XP mdb application. I'm still trying to figure out exactly what populates the Err object and the ADO Errors collection.

Are there any good books or references describing best practices for coding triggers and error handling in stored procedures?
0
Comment
Question by:rmk
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
hkamal earned 250 total points
ID: 11768616
Your approach to error-handling is spot on. Check @@error, raise an error, print a messge and rollback (I'd also RETURN -1)
A trigger is a stored proc attached toa table object; it should follow the same strict rules ergarding debug and error handling
Try "T-SQL Programming with Stored Procedures and Triggers" (Wells, I think). It contains a number of real-world scenarios, common problems and solutions
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 11768663
You need to be aware that if you ROLLBACk you will also rollback the original transaction, in this case the delete.
0
 
LVL 5

Expert Comment

by:hkamal
ID: 11770554
Scott, I think that is the idea, no? If I can't save what I'm deleting I'd want it undeleted ..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

685 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