HP 3000 Manuals

Preprocessing of Dynamic Non-Queries [ ALLBASE/SQL Pascal Application Programming Guide ] MPE/iX 5.0 Documentation


ALLBASE/SQL Pascal Application Programming Guide

Preprocessing of Dynamic Non-Queries 

There are two methods for dynamic preprocessing of a non-query:

   *   Using EXECUTE IMMEDIATE.
   *   Using PREPARE and EXECUTE.

The first method can be used with any non-query; the second is only for
those non-query commands that use sections at execution time.

Using EXECUTE IMMEDIATE 

If you know in advance that a dynamic command will not be a query, you
can dynamically preprocess and execute the command in one step, using the
EXECUTE IMMEDIATE command.  Figure 10-3  illustrates a procedure
hosting a dynamic UPDATE STATISTICS command that can be handled in this
fashion.

Procedure UpdateStatistics  1  prompts the user for a table name  2 .
The table name entered is assigned to the host variable CmdLine  3  to
complete the UPDATE STATISTICS command.  After the command is prepared
and executed  4 , the transaction is terminated with a COMMIT WORK
command  5  or a ROLLBACK WORK command  6 , depending on the value in
SQLCA.SQLCODE. Terminating the transaction before accepting another table
name and re-executing the UPDATE STATISTICS command releases any locks
obtained and improves concurrency.

If you do not know in advance whether a dynamic command will be a query
or a non-query, you must use the PREPARE command to dynamically
preprocess the command, the DESCRIBE command to distinguish between
queries and non-queries, and the EXECUTE or EXECUTE IMMEDIATE command to
execute the dynamic non-query.  The program examined later in this
chapter under "Program Using Dynamic Commands of Unknown Format"
illustrates how to handle this situation.
____________________________________________________________________________
|                                                                          |
|     .                                                                    |
|     .                                                                    |
|     .                                                                    |
|     var                                                                  |
|       EXEC SQL BEGIN DECLARE SECTION;                                    |
|       CmdLine         : String[100];                                     |
|       EXEC SQL END DECLARE SECTION;                                      |
|                                                                          |
|       TableName       : String[50];                                      |
|     .                                                                    |
|     .                                                                    |
|     .                                                                    |
|     procedure UpdateStatistics;                                        1 |
|     begin                                                                |
|                                                                          |
|     repeat                                                               |
|                                                                          |
|     prompt('Enter name of table or / to terminate > ');                2 |
|     readln(TableName);                                                   |
|     if TableName[1] <> '/' then                                          |
|       begin                                                              |
|         CmdLine := 'UPDATE STATISTICS FOR TABLE ' + TableName + ';';   3 |
|                                                                          |
|         EXEC SQL EXECUTE IMMEDIATE :CmdLine;                           4 |
|                                                                          |
|         if SQLCA.SQLCODE = 0 then                                        |
|           EXEC SQL COMMIT WORK;                                        5 |
|         else                                                             |
|           EXEC SQL ROLLBACK WORK;                                      6 |
|                                                                          |
|       end;                (* end of if TableName *)                      |
|     until TableName[1] = '/';                                            |
|                                                                          |
|     end;                  (* end of UpdateStatistics procedure *)        |
|     .                                                                    |
|     .                                                                    |
|     .                                                                    |
____________________________________________________________________________

          Figure 10-3.  Procedure Hosting Dynamic Non-Query Commands 

Using PREPARE and EXECUTE 

Use PREPARE command syntax to create and store a temporary section for
the dynamic command:

     PREPARE CommandName FROM CommandSource 

Because the PREPARE command operates only on sections, it can be used to
dynamically preprocess only SQL commands executed by using sections.  The
DBE session management and transaction management commands can only be
dynamically preprocessed by using EXECUTE IMMEDIATE.

With PREPARE, ALLBASE/SQL creates a temporary section for the command
that you can execute one or more times in the same transaction by using
the EXECUTE command:

     EXEC SQL PREPARE MyNonQuery FROM :DynamicCommand;

     for i := 1 to MaxIterations do
     EXEC SQL EXECUTE MyNonQuery;

As soon as you process a COMMIT WORK or ROLLBACK WORK command, the
temporary section is deleted.



MPE/iX 5.0 Documentation