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.

SELECT @Error = @@ERROR;<br />

-- Print out a blank separator line<br />

PRINT ‘’;<br />

-- The value of our holding variable is just what we would expect<br />

PRINT ‘The Value of @Error is ‘ + CONVERT(varchar, @Error);<br />

-- The value of @@ERROR has been reset - it’s back to zero<br />

-- since our last statement (the PRINT) didn’t have an error.<br />

PRINT ‘The Value of @@ERROR is ‘ + CONVERT(varchar, @@ERROR);<br />

Now execute your script and you can examine how @@ERROR is affected:<br />

Msg 547, Level 16, State 0, Line 4<br />

The INSERT statement conflicted with the FOREIGN KEY constraint<br />

“FK_BusinessEntityContact_Person_PersonID”. The conflict occurred in database<br />

“AdventureWorks<strong>2008</strong>”, table “Person.Person”, column ‘BusinessEntityID’.<br />

The statement has been terminated.<br />

The Value of @Error is 547<br />

The Value of @@ERROR is 0<br />

This illustrates pretty quickly the issue of saving the value from @@ERROR. The first error statement is<br />

only informational in nature. <strong>SQL</strong> <strong>Server</strong> has thrown that error, but hasn’t stopped the code from executing.<br />

Indeed, the only part of that message that your sproc has access to is the error number. That error<br />

number resides in @@ERROR for just that next T-<strong>SQL</strong> statement; after that, it’s gone.<br />

Using @@ERROR in a Sproc<br />

Chapter 12: Stored Procedures<br />

Notice that @Error and @@ERROR are two separate and distinct variables and can be<br />

referred to separately. This isn’t just because of the case difference. (Depending on<br />

how you have your server configured, case sensitivity can affect your variable names.)<br />

It’s because of the difference in scope. The @ or @@ is part of the name, so the number<br />

of @ symbols on the front makes each one separate and distinct from the other.<br />

OK, so let’s start with an assumption here: If you’re using @@ERROR, then the likelihood is that you are<br />

not using TRY/CATCH blocks. If you have not made this choice for backward compatibility reasons, I’m<br />

going to bop you upside the head and suggest you reconsider — TRY/CATCH is the much cleaner and allaround<br />

better way.<br />

TRY/CATCH will handle varieties of errors that in previous versions would have ended your script<br />

execution.<br />

That said, TRY/CATCH is out of the equation if backward compatibility with <strong>SQL</strong> <strong>Server</strong> 2000 or prior is<br />

what you need, so let’s take a quick look.<br />

What we’re going to do is look at two short procedures. Both are based on things we have already done<br />

in scripting or in earlier stored-procedure examples, but we want to take a look at how inline error<br />

381

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

Saved successfully!

Ooh no, something went wrong!