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.

www.it-ebooks.infoCHAPTER 32 • STORED ROUTINESEven though this example includes some new syntax (all of which will soon be introduced), it shouldbe rather straightforward.The remainder of this section is devoted to coverage of the syntax commonly used when creatingmultistatement stored routines.EFFECTIVE STORED ROUTINE MANAGEMENTStored routines can quickly become lengthy and complex, adding to the time required to create and debugtheir syntax. For instance, typing in the calculate_bonus procedure can be tedious, particularly if alongthe way you introduced a syntax error that required the entire routine to be entered anew. To alleviatesome of the tedium, insert the stored routine creation syntax into a text file, and then read that file into themysql client, like so:%>mysql [options] < calculate_bonus.sqlThe [options] string is a placeholder for your connection variables. Don’t forget to change over to theappropriate database before creating the routine by adding USE db_name; to the top of the script;otherwise, an error will occur.To modify an existing routine, you can change the file as necessary, delete the existing routine by usingDROP PROCEDURE (introduced later in this chapter), and then re-create it using the above process. Whilethere is an ALTER PROCEDURE statement (also introduced later in this chapter), it is presently only capableof modifying routine characteristics.Another very effective mechanism for managing routines is through <strong>MySQL</strong> Query Browser, introduced inChapter 27. Via the interface you can create, edit, and delete routines.The BEGIN and END BlockWhen creating multistatement stored routines, you need to enclose the statements in a BEGIN/END block.The block prototype looks like this:BEGINstatement 1;statement 2;...statement N;ENDNote that each statement in the block must end with a semicolon.ConditionalsBasing task execution on run-time information is key for wielding tight control over the outcome. Storedroutine syntax offers two well-known constructs for performing conditional evaluation: the IF-ELSEIF-ELSE statement and the CASE statement. Both are introduced in this section.635

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

Saved successfully!

Ooh no, something went wrong!