?
Solved

Best Practices for Error Handling in Triggers

Posted on 2004-08-10
3
Medium Priority
?
733 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
hkamal earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

777 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