Progress
Embedded SQL-92
Guide and Reference


PREPARE Statement

Parses and assigns a name to an ad hoc or dynamically generated SQL-92 statement for execution. You use a PREPARE statement in a series of steps that allows a program to accept or generate SQL-92 statements at run time.

SYNTAX

EXEC SQL PREPARE statement_name FROM statement_string ; 

statement_name

A name for the dynamically generated statement. The DESCRIBE Statements, EXECUTE Statement, and DECLARE CURSOR Statement refer to this statement_name. A statement_name must be unique in a program.

statement_string

Specifies the SQL-92 statement to be prepared for dynamic execution. You can either use the name of a C Language string variable containing the SQL-92 statement or specify the SQL-92 statement as a quoted literal.

SYNTAX
{  :host_variable  |  quoted_literal  }  

In either format, the statement string must be a character string that is a dynamically executable statement. If there is an SQL-92 syntax error, the PREPARE statement returns an error in the SQLCA.

EXAMPLES

The first example is a code fragment from the dynupd function in sample program 3DynUpd.pc, which illustrates dynamic processing of an UPDATE statement. The complete source for the sample program is listed in "ESQL-92 Sample Programs."

/*
** Process a dynamic non-SELECT input statement
**    PREPARE the statement
**    EXECUTE the prepared statement
**    COMMIT WORK
*/

EXEC SQL PREPARE dynstmt FROM :sql_stmt_v ;
EXEC SQL EXECUTE dynstmt ;
EXEC SQL COMMIT WORK ; 

This example is a code fragment from the dynsel function in sample program 4DynSel.pc, which illustrates dynamic processing of a SELECT statement. The complete source for the sample program is listed in "ESQL-92 Sample Programs."

/*
**    PREPARE a the dynamic SELECT statement.
**    DECLARE cursor for the prepared SELECT statement.
**    NOTE: You must set input parameter values before OPEN CURSOR.
**    If your query has input parameters, you must define them in
**    the DECLARE SECTION.
**    OPEN the declared cursor.
**    NOTE: For static statements, if a DECLARE CURSOR
**    statement contains references to automatic variables,
**    the OPEN CURSOR statement must be in the same C function.
**
**    Name WHENEVER routine for NOT FOUND condition.
**    FETCH a row and print results until no more rows.
*/

EXEC SQL PREPARE stmtid from :sel_stmt_v ;
EXEC SQL DECLARE dyncur CURSOR FOR stmtid ;
EXEC SQL OPEN dyncur ;            
EXEC SQL WHENEVER NOT FOUND GOTO seldone ; 

NOTES

AUTHORIZATION

The user executing this statement must have at least one of the following privileges:

SQL COMPLIANCE

SQL-92

ENVIRONMENT

Embedded SQL-92 only

RELATED STATEMENTS

EXECUTE Statement, EXECUTE IMMEDIATE Statement, OPEN Statement, CLOSE Statement, FETCH Statement, and SQLCA structure


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