Solved

Best Practices for Error Handling in Triggers

Posted on 2004-08-10
3
719 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:ScottPletcher
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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

18 Experts available now in Live!

Get 1:1 Help Now