17.07.2015 Views

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

Defensive Database Programming - Red Gate Software

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 3: Surviving Changes to <strong>Database</strong> ObjectsASBEGIN ;SELECT CustomerId ,FirstName ,LastName ,PhoneNumber ,StatusFROM dbo.CustomersWHERE LastName = COALESCE(@LastName, LastName)AND PhoneNumber = COALESCE(@PhoneNumber,PhoneNumber) ;END ;Listing 3-13: The SelectCustomersByName stored procedure.When we invoke this stored procedure, we can explicitly name its parameters, and makethe code more readable, but we are not forced to do so, as shown in Listing 3-14.EXEC dbo.SelectCustomersByName'Hansen',-- @LastName'(234)123-4567' ; -- @PhoneNumberEXEC dbo.SelectCustomersByName@LastName = 'Hansen',@PhoneNumber = '(234)123-4567' ;Listing 3-14: Two ways to invoke the SelectCustomersByName stored procedure.At the moment, either way of invoking the stored procedure produces the same result.Suppose, however, that the signature of this stored procedure is subsequently modifiedto accept an optional @FirstName parameter, as described in Listing 3-15.ALTER PROCEDURE dbo.SelectCustomersByName@FirstName VARCHAR(50) = NULL ,@LastName VARCHAR(50) = NULL ,@PhoneNumber VARCHAR(50) = NULL89

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

Saved successfully!

Ooh no, something went wrong!