prepare

Description

Declares a name for a dynamic SQL statement buffer.

Syntax

exec sql [at connection_name] prepare statement_name from 
         {:host_variable | "string"} end-exec

Parameters

statement_name

An identifier used to reference the statement. The statement_name must uniquely identify the statement buffer and must conform to the SQL identifier rules for naming variables. It can also be a host_variable string containing a valid SQL identifier. statement_name must not be longer than 255 characters.

host_variable

A character-string host variable that contains an executable SQL statement. Place dynamic parameter markers (“?”) anywhere in the select statement where a host variable value will be substituted.

string

A literal string that can be used in place of host_variable.

Examples

Example 1

 EXEC SQL BEGIN DECLARE SECTION END-EXEC.
           01     DEMO-BUFFER     PIC X(120).
           01     STATE          PIC X(3).
      EXEC SQL END DECLARE SECTION END-EXEC.
 
           ...
 
 * The 'select into table' statement returns no results
 * to the program, so it does not need a cursor.
 
      MOVE "select * into #work from authors where state = ?" TO
  -               DEMO-BUFFER.
 
      DISPLAY "STATE ? ".
      ACCEPT STATE.
 
      EXEC SQL PREPARE dynstmt FROM :DEMO-BUFFER END-EXEC.
      EXEC SQL EXECUTE dynstmt USING :STATE END-EXEC.
 
      EXEC SQL DEALLOCATE PREPARE dynstmt END-EXEC.

Usage

See also

declare cursor, execute, execute immediate, deallocate prepare