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.
- When you know the number and data types of the columns at compile
time, you can use host variables to hold the column values.
The following tables summarize how to execute the different
types of prepared SQL statements.
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 |
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, |
When number and data types of input parameters are not known | Yes | OPEN...USING SQL DESCRIPTOR OPEN...USING DESCRIPTOR |
Handling an unknown column list |
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, |
When number and data types of return values are not known | No | OPEN | 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 |
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 |
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 |
Execute a parameterized singleton SELECT statement |