Using Immediate Execution

You typically use immediate execution for any SQL statements that do not return data to your application and do not need to execute more than once per ESQL session. DDL statements are common candidates for immediate execution. But you can use this technique for any of the statements that are appropriate for immediate execution listed in the "Executing Dynamic ESQL" section in this chapter.

NOTE: SQL statements for immediate execution cannot contain dynamic parameter markers.

ESQL provides the EXECUTE IMMEDIATE statement to perform immediate execution using the following syntax:

SYNTAX
EXEC SQL EXECUTE IMMEDIATE request-string; 

request-string

A C character string expression specifying the SQL statement to execute. This character string must contain direct SQL. It cannot contain the EXEC SQL prefix, the semicolon (;) terminator, or embedded host variable names used in ESQL statements. See the "Differences Between Dynamic and Static ESQL" section for more information and additional restrictions on the request-string.

ESQL compiles the SQL statement in the request-string at run time, but does not save the resulting r-code. If the same statement is encountered again, it is recompiled.

The following code fragment shows an example using the EXECUTE IMMEDIATE statement:

long SQLCODE;
char *prequest;
            .
            .
            .
prequest = "DROP TABLE mytable";
EXEC SQL EXECUTE IMMEDIATE prequest; 

In this example, the character pointer request is set to the string "DROP TABLE mytable", a DDL statement. Then the EXECUTE IMMEDIATE statement executes the statement. The preprocessor converts this statement to a call to the sqldynrq() ESQL-LIB function:

static SQLRQHDL *sql0 = (SQLRQHDL *) 0;
            .
            .
            .
long SQLCODE;
char *prequest;
            .
            .
            .
prequest = "DROP TABLE mytable";
/* EXEC SQL EXECUTE IMMEDIATE prequest; */
{
    sqlcdbind(&SQLCODE, (char *)0);
    sqldynrq(&sql0,prequest);
} 

This function compiles and executes the SQL request. For information on the sqldynrq() function, see ESQL-LIB Reference."

NOTE: For statements that you can execute using immediate execution, you can also use prepared execution. See the "Using Prepared Execution" section in this chapter.


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095