17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15: Triggers<br />

FOR|AFTER<br />

The FOR (or, alternatively, you can use AFTER) clause indicates under what type of action(s) you want<br />

this trigger to fire. You can have the trigger fire whenever there is an INSERT, UPDATE, or DELETE, or any<br />

mix of the three. So, for example, your FOR clause could look something like:<br />

… or:<br />

… or:<br />

FOR INSERT, DELETE<br />

FOR UPDATE, INSERT<br />

FOR DELETE<br />

As stated in the section about the ON clause, triggers declared using the FOR or AFTER clause can be<br />

attached only to tables — no views are allowed (see INSTEAD OF triggers for those).<br />

INSERT Trigger<br />

The code for any trigger that you mark as being FOR INSERT will be executed anytime that someone<br />

inserts a new row into your table. For each row that is inserted, <strong>SQL</strong> <strong>Server</strong> will create a copy of that new<br />

row and insert it in a special table that exists only within the scope of your trigger. That table is called<br />

Inserted, and we’ll see much more of it over the course of this chapter. The big thing to understand is<br />

that the Inserted table lives only as long as your trigger does. Think of it as not existing before your<br />

trigger starts or after your trigger completes.<br />

DELETE Trigger<br />

This works much the same as an INSERT trigger does, save that the Inserted table will be empty (after<br />

all, you deleted rather than inserted, so there are no records for the Inserted table). Instead, a copy of<br />

each record that was deleted is inserted into another table called Deleted. That table, like the Inserted<br />

table, is limited in scope to just the life of your trigger.<br />

UPDATE Trigger<br />

More of the same, save for a twist. The code in a trigger declared as being FOR UPDATE will be fired<br />

whenever an existing record in your table is changed. The twist is that there’s no such table as UPDATED.<br />

Instead, <strong>SQL</strong> <strong>Server</strong> treats each row as if the existing record had been deleted and a totally new record<br />

was inserted. As you can probably guess from that, a trigger declared as FOR UPDATE contains not one<br />

but two special tables called Inserted and Deleted. The two tables have exactly the same number of<br />

rows, of course.<br />

WITH APPEND<br />

456<br />

WITH APPEND is something of an oddball and, in all honesty, you’re pretty unlikely to use it; nonetheless,<br />

we’ll touch on it briefly. WITH APPEND applies only when you are running in 6.5 compatibility<br />

mode (which can be set using sp_dbcmptlevel) — something that should be exceedingly rare at this<br />

point (it’s been out of date for nearly a decade now).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!