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 12: Stored Procedures<br />

<strong>SQL</strong> <strong>Server</strong> won’t perform this insert for you because there is a FOREIGN KEY constraint on both<br />

BusinessEntityID and PersonID that references other tables. Since there is not a matching record in<br />

both tables, the record we are trying to insert into Person.BusinessEntityContact violates both of<br />

those foreign key constraints and is rejected:<br />

Msg 547, Level 16, State 0, Line 1<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 />

Pay attention to that error 547 up there — that’s something of which we can make use.<br />

It’s worth noting that just the first foreign-key violation shows up in the error <strong>SQL</strong> <strong>Server</strong> provided.<br />

This is because <strong>SQL</strong> <strong>Server</strong> got to that error, saw it, and knew there was no point in going further. If we<br />

fixed the first error, then the second would be detected and we would again error out.<br />

Making Use of @@ERROR<br />

380<br />

We already talked some about this bad boy when we were looking at scripting, but it’s time to get a lot<br />

friendlier with this particular system function.<br />

To review, @@ERROR contains the error number of the last T-<strong>SQL</strong> statement executed. If the value is zero,<br />

then no error occurred. This is somewhat similar to the ERROR_NUMBER() function we saw in the last<br />

chapter when we first discussed TRY/CATCH blocks. While ERROR_NUMBER() is only valid within a<br />

CATCH block (and remains the same regardless of where you are within that CATCH block), @@ERROR<br />

receives a new value with each statement you execute.<br />

The caveat with @@ERROR is that it is reset with each new statement. This means that<br />

if you want to defer analyzing the value, or you want to use it more than once, you<br />

need to move the value into some other holding bin — a local variable that you have<br />

declared for this purpose.<br />

Play with this just a bit using the INSERT example from before:<br />

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

GO<br />

DECLARE @Error int;<br />

-- Bogus INSERT - there is no PersonID or BusinessEntityID of 0. Either of<br />

-- these could cause the error we see when running this statement.<br />

INSERT INTO Person.BusinessEntityContact<br />

(BusinessEntityID<br />

,PersonID<br />

,ContactTypeID)<br />

VALUES<br />

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

-- Move our error code into safekeeping. Note that, after this statement,<br />

-- @@Error will be reset to whatever error number applies to this statement

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

Saved successfully!

Ooh no, something went wrong!