Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

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

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;

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

Saved successfully!

Ooh no, something went wrong!