Execute the SQL statement

After an SQL statement is prepared, the database server can execute it. The way to execute a prepared statement depends on:
  • How many rows (groups of values) the SQL statement returns:
    • Statements that return one row of data include a singleton SELECT and an EXECUTE FUNCTION statement.
    • Statements that can return more than one row of data require a cursor to execute; they include a non-singleton SELECT and an EXECUTE FUNCTION statement.
    • All other SQL statements, including EXECUTE PROCEDURE, return no rows of data.

    For more information about how to execute statements that require cursors, see A database cursor.

  • Whether the statement has input parameters
    If so, the statement must be executed with the USING clause:
    • For SELECT and INSERT statements, use the OPEN...USING statement.
    • For non-SELECT statements, use the EXECUTE...USING statement.
  • Whether you know the data types of statement columns at compile time:
    • When you know the number and data types of the columns at compile time, you can use host variables to hold the column values.

      For more information, see SQL statements that are known at compile time.

    • When you do not know the number and data types of columns at compile time, you must use the DESCRIBE statement to define the column and a dynamic-management structure to hold the column values.

      For more information, see Determine SQL statements.

The following tables summarize how to execute the different types of prepared SQL statements.
Table 1. Executing prepared SQL statements that do not return rows (except INSERT, which is associated with a cursor)
Type of SQL statement Input parameters Statement to execute See
With no input parameters No EXECUTE Execute non-SELECT statements
When number and data types of input parameters are known Yes EXECUTE...USING An EXECUTE USING statement
When number and data types of input parameters are not known Yes EXECUTE...USING SQL DESCRIPTOR

EXECUTE...USING DESCRIPTOR

Handling a parameterized UPDATE or DELETE statement

Handling a parameterized UPDATE or DELETE statement

Table 2. Executing an INSERT statement that is associated with a cursor
Type of SQL statement Input parameters Statement to execute See
With no input parameters No OPEN Declare a select cursor
When number and data types of input parameters (insert columns) are known Yes OPEN...USING An OPEN USING statement,

Handling an unknown column list

When number and data types of input parameters are not known Yes OPEN...USING SQL DESCRIPTOR

OPEN...USING DESCRIPTOR

Handling an unknown column list

Handling an unknown column list

Table 3. Executing prepared SQL statements that can return more than one row: non-singleton SELECT, SPL function
Type of SQL statement Input parameters Statement to execute See
With no input parameters No OPEN Declare a select cursor
When number and data types of select-list columns are not known No OPEN Execute a SELECT that returns multiple rows,

Execute a SELECT that returns multiple rows

When number and data types of return values are not known No OPEN Executing a cursor function,

Executing a cursor function

When number and data types of input parameters are known Yes OPEN...USING An OPEN USING statement
When number and data types of input parameters are not known Yes OPEN...USING SQL DESCRIPTOR

OPEN...USING DESCRIPTOR

Execute a parameterized SELECT that returns multiple rows

Execute a parameterized SELECT that returns multiple rows

Table 4. Executing prepared SQL statements that return only one row: singleton SELECT, any external function, or an SPL function that returns only one group of values
Type of SQL statement Input parameters Statement to execute See
With no input parameters No EXECUTE...INTO The PREPARE and EXECUTE INTO statements
When number and data types of returned values are not known No EXECUTE...INTO DESCRIPTOR

EXECUTE...INTO SQL DESCRIPTOR

Handling an unknown select list

Execute a noncursor function

Handling an unknown select list

Execute a noncursor function

When number and data types of input parameters are known Yes EXECUTE...INTO ...USING An EXECUTE USING statement
When number and data types of input parameters are not known Yes

EXECUTE...INTO
...USING SQL DESCRIPTOR

EXECUTE...INTO
...USING DESCRIPTOR

Execute a parameterized singleton SELECT statement

Execute a parameterized singleton SELECT statement