prepare

The EGL prepare statement specifies an SQL PREPARE statement, which optionally includes details that are known only at run time. You run the prepared SQL statement by running an EGL execute statement or (if the SQL statement returns a result set) by running an EGL open or get statement.


Syntax diagram for the prepare statement

preparedStatementID

An identifier that relates the prepare statement to the execute, open, or get statement.

stringExpression

A string expression that contains a valid SQL statement.

SQL record name

Name of an SQL record. Specifying this name has two benefits:

  • The EGL editor provides a dialog to derive an SQL statement based on your specifications

  • After the prepare statement runs, you can test the record name against an I/O error value to determine whether the statement succeeded, as in the following example:
      try
        prepare prep01 from  
         "insert into " + aTableName + 
         "(empnum, empname) " +
         "value ?, ?"
        for empRecord;
    
      onException
        if empRecord is unique
          myErrorHandler(8);
        else
          myErrorHandler(12);
        end
      end

Another example is as follows:

  myString = 
    "insert into myTable " +  "(empnum, empname) " +
    "value ?, ?";

  try
    prepare myStatement 
      from myString;
  onException
    myErrorHandler(12);      // exits the program
  end

  try
    execute myStatement 
    using :myRecord.empnum,
          :myRecord.empname;
  onException
    myErrorHandler(15);
  end

As shown in the previous examples, the developer can use a question mark (?) where a host variable should appear. Then, the name of the host variable used at run time is placed in the using clause of the execute, open, or get statement that runs the prepared statement.

A prepare statement that acts on a row of a result set may include a phrase of the format where current of resultSetIdentifier. This technique is valid only in the following situation:

An example is as follows:

     prepare prep02 from
    "update myTable " +
    "set empname = ?,  empphone = ?  where current of x1" ;
 
  execute prep02 using empname, empphone ;
  freeSQL prep02;

Related concepts
References to parts
Record types and properties
SQL support

Related reference
add
close
delete
Exception handling
execute
freeSQL
get
get next
get previous
I/O error values
EGL statements
open
replace
SQL item properties
Text expressions
Syntax diagram