17.07.2015 Views

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

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 1: Basic <strong>Defensive</strong> <strong>Database</strong> <strong>Programming</strong> TechniquesSELECT i.ObjectID ,'SizeInInches' ,CURRENT_TIMESTAMP ,d.SizeInInchesFROM inserted AS iINNER JOIN deleted AS d ONi.ObjectID = d.ObjectIDWHERE i.SizeInInches d.SizeInInchesUNION ALLSELECT i.ObjectID ,'WeightInPounds' ,CURRENT_TIMESTAMP ,d.WeightInPoundsFROM inserted AS iINNER JOIN deleted AS d ONi.ObjectID = d.ObjectIDWHERE i.WeightInPounds d.WeightInPounds ;END ;-- after the body of the trigger completes,-- the original value of ROWCOUNT is restored-- by the database engineListing 1-13: Resetting ROWCOUNT at the start of the trigger.We can rerun the test from Listing 1-12, and this time the trigger will work as required,logging both changes. Note that the scope of our SET ROWCOUNT is the trigger, so ourchange will not affect the setting valid at the time when the trigger was fired.SET ROWCOUNT is deprecated in SQL Server 2008……and eventually, in some future version, will have no effect on INSERT, UPDATE orDELETE statements. Microsoft advises rewriting any such statements that rely onROWCOUNT to use TOP instead. As such, this example may be somewhat lessrelevant for future versions of SQL Server; the trigger might be less vulnerable tobeing broken, although still not immune. However, at the time of writing, thisexample is very relevant.36

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

Saved successfully!

Ooh no, something went wrong!