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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Now, here things get a bit sticky and versions become important, so hang with me as we go down a very<br />

much winding road. . . .<br />

As I write this, most <strong>SQL</strong> <strong>Server</strong> texts for <strong>2008</strong> are not out, but I’ll go ahead and venture the same guess<br />

that I made in the 2005 version of this book — that is, most beginning books will not discuss much in the<br />

way of prior versions. Indeed, I’ve generally avoided discussion of how things were done in prior versions<br />

as it just adds more complexity. That said, I’m going to touch on prior versions in this section. Why?<br />

Well, most database developers will work with code that predates <strong>SQL</strong> <strong>Server</strong> 2005 (when TRY/CATCH<br />

was first introduced). Well, there was no formal error handler in <strong>SQL</strong> <strong>Server</strong> 2000 and earlier.<br />

With this is mind, I’m going to give you a slimmed-down version of how error handling used to be — if<br />

for no other reason than to help you grasp the “why they did it that way” in older code you may come<br />

across. If you’re certain that you’re going to be a “<strong>SQL</strong> <strong>Server</strong> 2005 code or newer only” kinda DBA,<br />

then, by all means, feel free to skip this.<br />

One thing remains common between the old and new error handling models — higher-level runtime errors.<br />

It is possible to generate errors that will cause <strong>SQL</strong> <strong>Server</strong> to terminate the script immediately. This was<br />

true prior to TRY/CATCH, and it remains true even in the TRY/CATCH era. Errors that have enough severity<br />

to generate a runtime error are problematic from the <strong>SQL</strong> <strong>Server</strong> side of the equation. TRY/CATCH<br />

logic is a bit more flexible for some errors than what we had prior to <strong>SQL</strong> <strong>Server</strong> 2005, but we still have<br />

times where our sproc doesn’t even know that something bad happened because the sproc in question<br />

terminated immediately and without notice (at least not to the sproc itself) on the error. On the bright<br />

side, all the current data access object models pass through the message on such errors, so you know<br />

about them in your client application and can do something about them there.<br />

The Way We Were …<br />

In <strong>SQL</strong> <strong>Server</strong> 2000 and earlier, there was no formal error handler. You did not have an option that said,<br />

“If any error happens, go run this code over in this other spot.” Instead, we had to monitor for error conditions<br />

within our code and then decide what to do at the point we detected the error (possibly well<br />

after the actual error occurred).<br />

Handling Inline Errors<br />

Inline errors are those pesky little things where <strong>SQL</strong> <strong>Server</strong> keeps running as such, but hasn’t, for some<br />

reason, succeeded in doing what you wanted it to do. For example, try to insert a record into the Person<br />

.BusinessEntityContact table that doesn’t have a corresponding record in the BusinessEntity or<br />

Person table:<br />

USE AdventureWorks<strong>2008</strong>;<br />

GO<br />

INSERT INTO Person.BusinessEntityContact<br />

(BusinessEntityID<br />

,PersonID<br />

,ContactTypeID)<br />

VALUES<br />

(0,0,1);<br />

Chapter 12: Stored Procedures<br />

379

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

Saved successfully!

Ooh no, something went wrong!