Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
15 Triggers Ah, triggers. Triggers are cool, triggers are neat, and triggers are our friends. At the very same time, triggers are evil, triggers are ugly, and triggers are our enemy. In short, I am often asked, “Should I use triggers?” The answer is, like most things in SQL, it depends. There’s little that’s black and white in the wonderful world of SQL Server — triggers are definitely a very plain shade of gray. From a beginner’s point of view (and by this chapter in this book, I hope you’re a lot less of a beginner — but still . . . ), you really want to be certain you know what you’re doing before you go the triggers route, so sit back, listen, learn, and decide for yourself whether they are right for you. In this chapter, we’ll try to look at triggers in all of their colors — from black all the way to white and a whole lot in between. The main issues we’ll be dealing with include: ❑ What is a trigger? ❑ Using triggers for more flexible referential integrity ❑ Using triggers to create flexible data integrity rules ❑ Using INSTEAD OF triggers to create more flexible updatable views ❑ Other common uses for triggers ❑ Controlling the firing order of triggers ❑ Performance considerations By the time we’re done, you should have an idea of just how complex the decision about when and where not to use triggers is. You’ll also have an inkling of just how powerful and flexible they can be. Most of all, if I’ve done my job well, you won’t be a trigger extremist (which so many SQL Server people I meet are) with the distorted notion that triggers are evil and should never be used. Neither will you side with at the other end of the spectrum, who think that triggers are the solution to all
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
- Page 440 and 441: Chapter 12: Stored Procedures I’d
- Page 442 and 443: Chapter 12: Stored Procedures match
- Page 444 and 445: Chapter 12: Stored Procedures There
- Page 446 and 447: Chapter 12: Stored Procedures 408 f
- Page 449 and 450: 13 User-Defined Functions Well, her
- Page 451 and 452: types!), except for BLOBs, cursors,
- Page 453 and 454: We get back the same set as with th
- Page 455 and 456: AS RETURN (SELECT BusinessEntityID,
- Page 457 and 458: in your relational database. These
- Page 459 and 460: AS BEGIN ( EmployeeID int NOT NULL,
- Page 461 and 462: So, as you can see, we can actually
- Page 463 and 464: Despite being schema-bound, this on
- Page 465 and 466: 14 Transactions and Locks This is o
- Page 467 and 468: we are unable or do not want to com
- Page 469 and 470: Figure 14-1 Data needed Data in cac
- Page 471 and 472: Transaction 4 This transaction wasn
- Page 473 and 474: Oops — problem!!! Transaction 2 h
- Page 475 and 476: The only cure for this is setting y
- Page 477 and 478: Exclusive Locks Exclusive locks are
- Page 479 and 480: Also: ❑ The Sch-S is compatible w
- Page 481 and 482: The syntax for switching between th
- Page 483 and 484: As with most things in life, howeve
- Page 485 and 486: purchased. Process 2 records sales;
- Page 487: Chapter 14: Transactions and Locks
- Page 491 and 492: ON While triggers take no parameter
- Page 493 and 494: Statement issued: “INSERT INTO...
- Page 495 and 496: SQL Server 6.5 and prior did not al
- Page 497 and 498: Let’s go ahead and test out our h
- Page 499 and 500: I just picked out “Adjustable Rac
- Page 501 and 502: happen the second time around. Recu
- Page 503 and 504: The syntax of sp_settriggerorder lo
- Page 505 and 506: Another upshot of this part-of-the-
- Page 507 and 508: 5, and 7, then you need to add the
- Page 509 and 510: that exercises the trigger we most
- Page 511 and 512: 16 A Brief XML Primer So, here we a
- Page 513 and 514: So, this might have you asking the
- Page 515 and 516: The declaration is made with a spec
- Page 517 and 518: the scope of any particular XML doc
- Page 519 and 520: As a side note, you can perform a r
- Page 521 and 522: If we have more than one customer,
- Page 523 and 524: The sql data type
- Page 525 and 526: XML functionality was a relatively
- Page 527 and 528: This is just a little confusing, so
- Page 529 and 530: Chapter 16: A Brief XML Primer
- Page 531 and 532: The result is rather verbose, so I
- Page 533 and 534: If, for example, we wanted to incre
- Page 535 and 536: 7 30 1.00 7 45 0.50 7 50 3.00 7 60
- Page 537 and 538: FOR XML provides three different in
15<br />
Triggers<br />
Ah, triggers. Triggers are cool, triggers are neat, and triggers are our friends. At the very same time,<br />
triggers are evil, triggers are ugly, and triggers are our enemy. In short, I am often asked, “Should I<br />
use triggers?” The answer is, like most things in <strong>SQL</strong>, it depends. There’s little that’s black and<br />
white in the wonderful world of <strong>SQL</strong> <strong>Server</strong> — triggers are definitely a very plain shade of gray.<br />
From a beginner’s point of view (and by this chapter in this book, I hope you’re a lot less of a<br />
beginner — but still . . . ), you really want to be certain you know what you’re doing before you go<br />
the triggers route, so sit back, listen, learn, and decide for yourself whether they are right for you.<br />
In this chapter, we’ll try to look at triggers in all of their colors — from black all the way to white<br />
and a whole lot in between. The main issues we’ll be dealing with include:<br />
❑ What is a trigger?<br />
❑ Using triggers for more flexible referential integrity<br />
❑ Using triggers to create flexible data integrity rules<br />
❑ Using INSTEAD OF triggers to create more flexible updatable views<br />
❑ Other common uses for triggers<br />
❑ Controlling the firing order of triggers<br />
❑ Performance considerations<br />
By the time we’re done, you should have an idea of just how complex the decision about when<br />
and where not to use triggers is. You’ll also have an inkling of just how powerful and flexible they<br />
can be.<br />
Most of all, if I’ve done my job well, you won’t be a trigger extremist (which so many <strong>SQL</strong> <strong>Server</strong><br />
people I meet are) with the distorted notion that triggers are evil and should never be used. Neither<br />
will you side with at the other end of the spectrum, who think that triggers are the solution to all