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.

HireDate = @HireDate,<br />

CurrentFlag = @CurrentFlag<br />

WHERE BusinessEntityID = @BusinessEntityID;<br />

INSERT INTO HumanResources.EmployeePayHistory<br />

(BusinessEntityID,<br />

RateChangeDate,<br />

Rate,<br />

PayFrequency)<br />

VALUES (@BusinessEntityID, @RateChangeDate, @Rate, @PayFrequency);<br />

COMMIT TRANSACTION;<br />

END TRY<br />

BEGIN CATCH<br />

-- Rollback any active or uncommittable transactions before<br />

-- inserting information in the ErrorLog<br />

IF @@TRANCOUNT > 0<br />

BEGIN<br />

ROLLBACK TRANSACTION;<br />

END<br />

EXECUTE dbo.uspLogError;<br />

END CATCH;<br />

END;<br />

What’s going on in this sproc is pretty straightforward: There are two statements (one to update the<br />

existing employee record and one to handle the additional history record) plus a very generic error handler.<br />

What we’re going to do is add some new code to this sproc to recognize some errors that might<br />

occur and provide return values that will notify the client of more specific error information. We’re not<br />

going to take the time to trap every potential error here, but we will catch some basics just to show how<br />

things might work. (Feel free to explore further on your own.)<br />

The error handler in this sproc is very generic and doesn’t really do anything that is specific to this particular<br />

sproc, so let’s start by considering some of the errors that might occur in this sproc, for example:<br />

❑ An Employee Whose BusinessEntityID doesn’t already exist: The UPDATE statement in the<br />

sproc will actually run just fine (no errors) without a valid BusinessEntityID. It will just fail to<br />

find a match and wind up affecting zero rows; the error here is logical in nature, and <strong>SQL</strong> <strong>Server</strong><br />

will see no problem with it at all. We should detect the error ourselves and trap it at this point<br />

before the sproc continues (since there is a foreign key between EmployeePayHistory and<br />

Employee, <strong>SQL</strong> <strong>Server</strong> will wind up raising an error on the INSERT statement when it can’t find<br />

a matching BusinessEntityID).<br />

❑ Two updates affecting the same BusinessEntityID at the same RateChangeDate: Again, the<br />

UPDATE statement will have no issues with such an update, but the INSERT statement will (the<br />

primary key for EmployeePayHistory is the composite of BusinessEntityID and RateChangeDate).<br />

Let’s address each of these in a new version of the sproc.<br />

Chapter 12: Stored Procedures<br />

First, let’s lay some groundwork. While <strong>SQL</strong> <strong>Server</strong> doesn’t really have the concept of a constant, I’m<br />

going to use some variables as though they are constants. By doing so, I’m going to get away from just<br />

385

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

Saved successfully!

Ooh no, something went wrong!