Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
326 Chapter 16: Dynamic SQL Each time you EXECUTE IMMEDIATE the same statement, it must be scanned for syntax errors again. Therefore, if you need to execute a dynamic SQL statement repeatedly, you will get better performance if you can have the syntax checked once and save the statement in some way. 1 Dynamic SQL with Dynamic Parameters If you want to repeat a dynamic SQL statement or if you need to use dynamic parameters (as you would to process the form in Figure 16-1), you need to use a more involved technique for preparing and executing your commands. The processing for creating and using a repeatable dynamic SQL statement is as follows: 1. Store the SQL statement in a host language string variable using host language variables for the dynamic parameters. 2. Allocate SQL descriptor areas. 3. Prepare the SQL statement. This process checks the statement for syntax and assigns it a name by which it can be referenced. 4. Describe one of the descriptor areas as input. 5. Set input parameters, associating each input parameter with the input parameter descriptor. 6. (Required only when using a cursor) Declare the cursor. 7. (Required only when using a cursor) Open the cursor. 8. Describe another descriptor area as output. 1 A few DBMSs (for example, DB2 for Z/OS) get around this problem by performing dynamic statement caching (DSC), where the DBMS saves the syntax-scanned/prepared statement and retrieves it from the cache if used again.