Solved

Best Practices for Error Handling in Triggers

Posted on 2004-08-10
3
718 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Ranking Based On Value 3 28
Help with SQL Query 23 39
Concatenating multiple comments into one row 16 37
Calculating Business Hours 19 59
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

772 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

12 Experts available now in Live!

Get 1:1 Help Now