11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 32 • STORED ROUTINESwww.it-ebooks.infoIF-ELSEIF-ELSEThe IF-ELSEIF-ELSE statement is one of the most common means for evaluating conditional statements.In fact, even if you’re a novice programmer, you’ve likely already used it on numerous occasions.Therefore, this introduction should be quite familiar. The prototype looks like this:IF condition THEN statement_list[ELSEIF condition THEN statement_list][ELSE statement_list]END IFFor example, suppose you modified the previously created calculate_bonus stored procedure todetermine the bonus percentage based on not only sales but also the number of years the salespersonhas been employed at the company:IF years_employed < 5 THENSET bonus = total * .05;ELSEIF years_employed >= 5 and years_employed < 10 THENSET bonus = total * .06;ELSEIF years_employed >=10 THENSET bonus = total * .07;END IFCASEThe CASE statement is useful when you need to compare a value against an array of possibilities. Whiledoing so is certainly possible using an IF statement, the code readability improves considerably by usingthe CASE statement. Its prototype looks like this:CASEWHEN condition THEN statement_list[WHEN condition THEN statement_list][ELSE statement_list]END CASEConsider the following example, which sets a variable containing the appropriate sales tax rate bycomparing a customer’s state to a list of values:CASEWHEN state="AL" THEN:SET tax_rate = .04;WHEN state="AK" THEN:SET tax_rate = .00;...WHEN state="WY" THEN:SET tax_rate = .04;END CASE;Alternatively, you can save some typing by using the following variation:636

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

Saved successfully!

Ooh no, something went wrong!