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 388 -- Set up "constants" for error codes DECLARE @BUSINESS_ENTITY_ID_NOT_FOUND int = -1000, @DUPLICATE_RATE_CHANGE int = -2000; BEGIN TRY BEGIN TRANSACTION; 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 COMMIT TRANSACTION; END TRY 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; END;
Go ahead and run this once: DECLARE @Return int; EXEC @Return = HumanResources.uspEmployeeHireInfo2 @BusinessEntityID = 1, @JobTitle = ‘His New Title’, @HireDate = ‘1996-07-01’, @RateChangeDate = ‘2008-07-31’, @Rate = 15, @PayFrequency = 1, @CurrentFlag = 1; SELECT @Return; And everything seems to run fine, but execute it a second time and we get some different results: Duplicate Rate Change Found ----------- -2000 (1 row(s) affected) We tried to insert a second row with the same pay history, but SQL Server wouldn’t allow that. We’ve used PRINT to supply informative output in case the statement is being executed without the Query window, and we’re outputting a specific return value that the client can match against a value in a resource list. Now, let’s try the same basic test, but use an invalid BusinessEntityID: DECLARE @Return int; EXEC @Return = HumanResources.uspEmployeeHireInfo2 @BusinessEntityID = 99999, @JobTitle = ‘My Invalid Employee’,; @HireDate = '2008-07-31', @RateChangeDate = ‘2008-07-31’, @Rate = 15, @PayFrequency = 1, @CurrentFlag = 1; SELECT @Return; We get a similar error message and return code, but each is slightly different to allow for the specific error detected: BusinessEntityID Not Found ----------- -1000 (1 row(s) affected) Chapter 12: Stored Procedures 389
- 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 424 and 425: Chapter 12: Stored Procedures 386 r
- 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
- Page 475 and 476: The only cure for this is setting y
Chapter 12: Stored Procedures<br />
388<br />
-- Set up "constants" for error codes<br />
DECLARE @BUSINESS_ENTITY_ID_NOT_FOUND int = -1000,<br />
@DUPLICATE_RATE_CHANGE int = -2000;<br />
BEGIN TRY<br />
BEGIN TRANSACTION;<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<br />
(@BusinessEntityID,<br />
@RateChangeDate,<br />
@Rate,<br />
@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 />
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 />
IF ERROR_NUMBER() = 2627 -- Primary Key violation<br />
BEGIN<br />
PRINT 'Duplicate Rate Change Found';<br />
RETURN @DUPLICATE_RATE_CHANGE;<br />
END<br />
END CATCH;<br />
END;