Question Locked

Trapping Updated Records

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Can a SQL Trigger be created on a Table that will determine which record in that Table was actually updated?

9 replies

by tony hopkinson

When an Update trigger fires
the the changed record(s) are in the deleted table and the new version(s) of them in the Inserted table.

At this point you have access to the primary key value(s) in the triggered table.

Obviously you can't use this data to twiddle with the table with the trigger on it, that would trigger another trigger ... :D
You can mess with inserted for validations, data cleaning and of course logging and cascading, (eg force creating a blank order line on a new order or some such).

A tip with triggers they are very nice, very powerful and a really quick route to the deep brown stuff if you go mad with them.

by zeroesque

"Obviously you can't use this data to twiddle with the table with the trigger on it, that would trigger another trigger ... "

I believe recursive triggers are turned off by default at the database level in SQL Server.

EXEC sp_dboption '', 'recursive triggers'

by tony hopkinson

Limit is 32 I think.

Hit that once by accident Table1 insert trigger to Table 2 , Table2 to Table1 :p

You get told off for that one now I believe.

Haven't done it since.

One of the classics is to get a trigger to send an email, which can take a heck of a long time.

Triggers are great as long as you remember a change isn't complete until all the triggers have returned.

SQL 2005 has some great options for using MSMQ, or the old stand by of having an action table where you store what happened and deal with it later can be used.
I used to use that method a lot, simple and very robust.

If at all possible avoid chaining triggers is always my advice.

by howyue

u can easily catch the updated row in withing the trigger itself, such as select * from inserted

by Member 22070467

Like it was mentioned earlier in this blog, you can track what is inserted/updated/deleted using the INSERTED and DELETED tables in the trigger. Triggers are off by default, so as long as you aren't getting crazy and updating other tables and create circular references then you won't have to worry about the triggers. Another option for you is to use the new OUTPUT clause in 2005. You can catch the values from the inserted/deleted tables in the same statement you use to update/insert/delete the data.

by Member 22070467

Ive actually never ran into a situation where I have created circular references with triggers, and I've written a lot of them. I don't particularly love triggers, but they can be very useful when necessary...such as checking for business rules after data has been updated. Such as not allowing certain thresholds to be reached in the database.

As in reference to your MSMQ comment...are you referring to Service Broker? If not, have you used SB? It is very handy for doing just what you described, but in the database.

by tony hopkinson

I have some vague memory from the course I did, underlying tech was MSMQ. However the point was that you could easily generate asynchronous tasks.

I used triggers to fill tables of items changed, then an other task bundled them up and FTP'd them to a HP Mini where the legacy ordering system was for instance.

Not something you want to wait around for in the middle of an update. :D

Nowadays I'd use one of the built in mechanisms, back in SQL Server 6.0 days the support wasn't good enough and the tech too fragile to risk.

The advantage of doing it in triggers is of course you don't have to worry about, 'queueing' the task explicitly, so it never gets missed by a coder.

Th reason I really liked them was for monitoring, and generating adhoc reports.

They are a good way of avoiding having to use cursors when you want to capture state changes between updates and such.

Who or what made a change to a particular pattern of data

A little dash board table, to collect simple stats on what's been without running a bunch of aggregate queries on your extermely dynamic tables.

Not doing any of this now all, on the coding side.

That's going to change soon though, just been given a new project, total revamp of the existing database schema.

Have to brush my skills up again, bit rusty.
:(

by Member 22070467

One thing that I am using SB for right now is to do almost exactly what you are talking about. I use triggers (the same script for every table) that packages the inserted and deleted items up into an xml variable and then pass that variable to a queue that processes it in the background. As soon as the inserted and deleted tables have been packaged and queued, the trigger returns.

This discussion is closed to new replies.