HP 3000 Manuals

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


ALLBASE/SQL C 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 8-3 illustrates a procedure hosting a
dynamic UPDATE STATISTICS command that can be handled in this fashion.

Function 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 Type"
illustrates how to handle this situation.
_____________________________________________________________________________
|                                                                           |
|     .                                                                     |
|     .                                                                     |
|     .                                                                     |
|       EXEC SQL BEGIN DECLARE SECTION;                                     |
|       char              CmdLine[100];                                     |
|       EXEC SQL END DECLARE SECTION;                                       |
|                                                                           |
|       char              TableName[50];                                    |
|     .                                                                     |
|     .                                                                     |
|     .                                                                     |
|     int UpdateStatistics()                                              1 |
|     {                                                                     |
|                                                                           |
|     do {                                                                  |
|         sprintf(CmdLine,"UPDATE STATISTICS FOR TABLE ");                  |
|                                                                           |
|     printf("\n Enter name of table or / to terminate > ");              2 |
|     gets(TableName);                                                      |
|     if (TableName[0] != '/') {                                            |
|                                                                           |
|         sprintf(CmdLine+strlen(CmdLine)," %s;",TableName);              3 |
|                                                                           |
|         EXEC SQL EXECUTE IMMEDIATE :CmdLine;                            4 |
|                                                                           |
|         if (sqlca.sqlcode == 0)  {                                        |
|           EXEC SQL COMMIT WORK;                                         5 |
|           }                                                               |
|         else                                                              |
|           EXEC SQL ROLLBACK WORK;                                       6 |
|                                                                           |
|       }                 /* End of if TableName */                         |
|     } while (TableName[0] != '/');                                        |
|                                                                           |
|     }                  /* End of UpdateStatistics function */             |
|     .                                                                     |
|     .                                                                     |
|     .                                                                     |
_____________________________________________________________________________

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

Using PREPARE and EXECUTE 

Use the PREPARE command 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=0;i<MaxIterations;i++)
     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