3

I'm trying to code a SQL Server 2000 trigger.

For each insert statement, I would like to log the inserted value into a log table, and then do some processing. How can I prevent the logging from being rolled back when the processing fails?

That's it seems I need an inner autonomous transaction to put the logging inside it. Is it doable with T-SQL on SQL Server 2000?

I hope the implementation does not cause too much overhead. Or if the processing fail, how can I gracefully manage it and do not make it rollback the origin insert. thanks

0

1 Answer 1

2

I would strongly recommend not to do any processing of any kind in a trigger.

You cannot control when and how often a trigger is fired - therefore, make sure your triggers are very lean - at most, insert a row (or a few rows) into an "audit" or "job to process" table, as a "marker" for later processing. But don't actually start/do the processing right in the trigger.

Handle the actual processing separately, using e.g. a SQL Agent Job or something. Since that'll be run independently of your original statement, you won't have any issues with "nested" transactions and stuff like that, either.

Sign up to request clarification or add additional context in comments.

2 Comments

,thanks for ur suggestions. any advice on how to capture all events attempting to modify data of a certain table, even if the attempt fails.
Wrong start assumption... make sure the trigger will NEVER fail. An error in a trigger actually makes it impossible to update/insert/delete the data. So... if you want to log AS WELL as process the log make the recording and the processing independent. Fill the log from within the trigger (that's the place to guarantee that logging will always be done), then OR process the log time-based OR have a look at service brokers (send a message from within the trigger). The latter will AND guarantee indecency AND guarantee immediate processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.