MySQL

Page 254

IMMEDIATE EXECUTION

237

logic of the code examines the values the user entered and builds a syntactically correct SQL UPDATEstatement. By using the dynamic SQL, the program can update just those columns for which the user has supplied new data. (Columns whose fields on the data entry form are left empty aren't added to the SQL statement.) S t r l n g theSQL; theSQL = "UPDATE c u s t o m e r s SET "~ b o o l e a n needsComma = f a l s e ; if

{

(valid_contents_in_street_field) theSQL = theSQL + " c u s t o m e r s t r e e t contents of street fleld; needsComma = t r u e ; m

} if

{

= " +

(valid_contents_in_city_field) if

} if

{

(needsComma) theSQL = theSQL + " , " ; theSQL = theSQL + " c u s t o m e r _ c i t y needsComma = t r u e ;

= " + contents_of_clty_fleld;

(valld_cont ent s_ln_st at e_f leld) •

} if

{

(needsComma) theSQL = theSQL + " , " ; theSQL = theSQL + " c u s t o m e r s t a t e content s_of_state_f leld ; needsComma = t r u e ; (valld_cont if

}

= " +

ent s_ln_zlp_f leld)

(needsCommas)

theSQL = " c u s t o m e r _ z i p

theSQL = theSQL + " , = " + contents_of_zip_field;

";

EXEC SQL EXECUTE IMMEDIATE "theSQL

Figure 13-2: Pseudocode to process a dynamic SQL update

There are two major limitations to EXECUTE IMMEDIATE: The SQL command cannot contain input parameters or output parameters. This means that you can't use SELECT o r FETCH statements.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.