Solved

Best Practices for Error Handling in Triggers

Posted on 2004-08-10
3
720 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

816 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

8 Experts available now in Live!

Get 1:1 Help Now