Progress
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

PREPARE statement_name FROM statement_string ; 

statement_name

A name for the dynamically generated statement. DESCRIBE, EXECUTE, and DECLARE CURSOR statements 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 use either the name of a C Language string variable containing the SQL-92 statement, or you can specify the SQL-92 statement as a quoted literal. If there is an SQL-92 syntax error, the PREPARE statement returns an error in the SQLCA.

SYNTAX
{ :host_variable | quoted_literal }  

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:

/* 
** 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:

/* 
**    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 ; 

The complete source for four sample programs are listed in Appendix A of the Progress Embedded SQL-92 Guide and Reference .

NOTES

AUTHORIZATION

Must have DBA privilege or Authorization for SQL-92 statement being prepared (See the AUTHORIZATION section for the relevant statement).

SQL COMPLIANCE

SQL-92

ENVIRONMENT

Embedded SQL-92 only

RELATED STATEMENTS

EXECUTE 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