Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Chapter 12: Stored Procedures 386 returning numbers and, instead, return a variable name that will make my code more readable by indicating the nature of the error I’m returning. … … SET NOCOUNT ON; -- Set up “constants” for error codes DECLARE @BUSINESS_ENTITY_ID_NOT_FOUND int, @DUPLICATE_RATE_CHANGE int SET @BUSINESS_ENTITY_ID_NOT_FOUND = -1000 SET @DUPLICATE_RATE_CHANGE = -2000 BEGIN TRY … … You may be curious as to why I’m using negative values here for my errors. While there is no real standard on such things, I tend to use positive values for return codes that are informational in nature (perhaps there are multiple possible successful outcomes and I want to indicate which successful outcome occurred) and negative values for errors. You can find your own path on such things; just make sure you follow the cardinal rule — be consistent! Also, I am deliberately not using the initialization syntax that became available in SQL Server 2008 (I would change the declare to DECLARE @BUSINESS_ENTITY_ ID_NOT_FOUND int = -1000). This is purely for backward compatibility reasons, so adjust accordingly. Next, we can test how many rows were affected by the UPDATE to HumanResources.Employee and utilize that to detect a BusinessEntityID Not Found error: … … UPDATE HumanResources.Employee SET JobTitle = @JobTitle, HireDate = @HireDate, CurrentFlag = @CurrentFlag WHERE BusinessEntityID = @BusinessEntityID; IF @@ROWCOUNT > 0 -- things happened as expected INSERT INTO HumanResources.EmployeePayHistory (BusinessEntityID, RateChangeDate, Rate, PayFrequency) VALUES (@BusinessEntityID, @RateChangeDate, @Rate, @PayFrequency); ELSE -- ruh roh, the update didn’t happen, so skip the insert, -- set the return value and exit BEGIN PRINT ‘BusinessEntityID Not Found’; ROLLBACK TRAN; RETURN @BUSINESS_ENTITY_ID_NOT_FOUND; END … …
Note the removal of the HireDate column from the UPDATE. As we discussed earlier, the RETURN will immediately exit the sproc supplying the return value provided (in this case, –1000 — the amount matching our variable). Our client application can now test the return value and match it against a known list of possible errors. That moves us on to our second potential error. We have a couple of ways we can handle this. We could pre-query the EmployeePayHistory table to see if it already has a matching row, and then avoid the INSERT entirely. Alternatively, we can just allow SQL Server to detect the error and just beef up the error handler to address that known possibility. In this case, I’m going to opt for the latter. It is almost always better to treat the rule and handle the exception. We would like to think that this particular error is going to be very infrequent, so we’ll largely assume it isn’t going to happen and address it when it does. With this in mind, we only need to make some alterations to our error handler: … … BEGIN CATCH -- Rollback any active or uncommittable transactions before -- inserting information in the ErrorLog IF @@TRANCOUNT > 0 BEGIN ROLLBACK TRANSACTION; END EXECUTE dbo.uspLogError; IF ERROR_NUMBER() = 2627 -- Primary Key violation BEGIN PRINT ‘Duplicate Rate Change Found’; RETURN @DUPLICATE_RATE_CHANGE; END END CATCH; … … Chapter 12: Stored Procedures OK, so with all these changes in place, let’s take a look at our new overall sproc. While this is a new sproc, I’m highlighting only those lines that change versus the original we cloned it from: CREATE PROCEDURE HumanResources.uspEmployeeHireInfo2 @BusinessEntityID [int], @JobTitle [nvarchar](50), @HireDate [datetime], @RateChangeDate [datetime], @Rate [money], @PayFrequency [tinyint], @CurrentFlag [dbo].[Flag] WITH EXECUTE AS CALLER AS BEGIN SET NOCOUNT ON; 387
- Page 373 and 374: DECLARE @RowCount int; --Notice the
- Page 375 and 376: When the editing tool encounters a
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389 and 390: -- Now we’re run our conditional
- Page 391 and 392: Out of the condition from inner con
- Page 393 and 394: A Simple CASE A simple CASE takes a
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
- Page 403: Chapter 11: Writing Scripts and Bat
- Page 406 and 407: Chapter 12: Stored Procedures Creat
- Page 408 and 409: Chapter 12: Stored Procedures Dropp
- Page 410 and 411: Chapter 12: Stored Procedures Suppl
- Page 412 and 413: Chapter 12: Stored Procedures 374 [
- Page 414 and 415: Chapter 12: Stored Procedures Confi
- Page 416 and 417: Chapter 12: Stored Procedures Now,
- Page 418 and 419: Chapter 12: Stored Procedures SQL S
- Page 420 and 421: Chapter 12: Stored Procedures 382 c
- Page 422 and 423: Chapter 12: Stored Procedures It wo
- Page 426 and 427: Chapter 12: Stored Procedures 388 -
- Page 428 and 429: Chapter 12: Stored Procedures Note
- Page 430 and 431: Chapter 12: Stored Procedures 392 n
- Page 432 and 433: Chapter 12: Stored Procedures All t
- Page 434 and 435: Chapter 12: Stored Procedures Sproc
- Page 436 and 437: Chapter 12: Stored Procedures When
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
- Page 440 and 441: Chapter 12: Stored Procedures I’d
- Page 442 and 443: Chapter 12: Stored Procedures match
- Page 444 and 445: Chapter 12: Stored Procedures There
- Page 446 and 447: Chapter 12: Stored Procedures 408 f
- Page 449 and 450: 13 User-Defined Functions Well, her
- Page 451 and 452: types!), except for BLOBs, cursors,
- Page 453 and 454: We get back the same set as with th
- Page 455 and 456: AS RETURN (SELECT BusinessEntityID,
- Page 457 and 458: in your relational database. These
- Page 459 and 460: AS BEGIN ( EmployeeID int NOT NULL,
- Page 461 and 462: So, as you can see, we can actually
- Page 463 and 464: Despite being schema-bound, this on
- Page 465 and 466: 14 Transactions and Locks This is o
- Page 467 and 468: we are unable or do not want to com
- Page 469 and 470: Figure 14-1 Data needed Data in cac
- Page 471 and 472: Transaction 4 This transaction wasn
- Page 473 and 474: Oops — problem!!! Transaction 2 h
Chapter 12: Stored Procedures<br />
386<br />
returning numbers and, instead, return a variable name that will make my code more readable by indicating<br />
the nature of the error I’m returning.<br />
…<br />
…<br />
SET NOCOUNT ON;<br />
-- Set up “constants” for error codes<br />
DECLARE @BUSINESS_ENTITY_ID_NOT_FOUND int,<br />
@DUPLICATE_RATE_CHANGE int<br />
SET @BUSINESS_ENTITY_ID_NOT_FOUND = -1000<br />
SET @DUPLICATE_RATE_CHANGE = -2000<br />
BEGIN TRY<br />
…<br />
…<br />
You may be curious as to why I’m using negative values here for my errors. While there is no real<br />
standard on such things, I tend to use positive values for return codes that are informational in nature<br />
(perhaps there are multiple possible successful outcomes and I want to indicate which successful outcome<br />
occurred) and negative values for errors. You can find your own path on such things; just make sure you<br />
follow the cardinal rule — be consistent! Also, I am deliberately not using the initialization syntax that<br />
became available in <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong> (I would change the declare to DECLARE @BUSINESS_ENTITY_<br />
ID_NOT_FOUND int = -1000). This is purely for backward compatibility reasons, so adjust accordingly.<br />
Next, we can test how many rows were affected by the UPDATE to HumanResources.Employee and utilize<br />
that to detect a BusinessEntityID Not Found error:<br />
…<br />
…<br />
UPDATE HumanResources.Employee<br />
SET JobTitle = @JobTitle,<br />
HireDate = @HireDate,<br />
CurrentFlag = @CurrentFlag<br />
WHERE BusinessEntityID = @BusinessEntityID;<br />
IF @@ROWCOUNT > 0<br />
-- things happened as expected<br />
INSERT INTO HumanResources.EmployeePayHistory<br />
(BusinessEntityID,<br />
RateChangeDate,<br />
Rate,<br />
PayFrequency)<br />
VALUES (@BusinessEntityID, @RateChangeDate, @Rate, @PayFrequency);<br />
ELSE<br />
-- ruh roh, the update didn’t happen, so skip the insert,<br />
-- set the return value and exit<br />
BEGIN<br />
PRINT ‘BusinessEntityID Not Found’;<br />
ROLLBACK TRAN;<br />
RETURN @BUSINESS_ENTITY_ID_NOT_FOUND;<br />
END<br />
…<br />
…