DB2 Version 9.7 for Linux, UNIX, and Windows

SQLExecDirect function (CLI) - Execute a statement directly

Purpose

Specification: CLI 1.1 ODBC 1.0 ISO CLI

SQLExecDirect() directly executes the specified SQL statement or XQuery expression using the current values of the parameter marker variables if any parameters exist in the statement. The statement or expression can only be executed once.

For XQuery expressions, you cannot specify parameter markers in the expression itself. You can, however, use the XMLQUERY function to bind parameter markers to XQuery variables. The values of the bound parameter markers will then be passed to the XQuery expression specified in XMLQUERY for execution.

Unicode equivalent: This function can also be used with the Unicode character set. The corresponding Unicode function is SQLExecDirectW(). Refer to Unicode functions (CLI) for information on ANSI to Unicode function mappings.

Syntax

SQLRETURN   SQLExecDirect (
               SQLHSTMT          StatementHandle,         /* hstmt */
               SQLCHAR           *StatementText,          /* szSqlStr */
               SQLINTEGER        TextLength);             /* cbSqlStr */

Function arguments

Table 1. SQLExecDirect arguments
Data type Argument Use Description
SQLHSTMT StatementHandle input Statement handle. There must not be an open cursor associated with StatementHandle.
SQLCHAR * StatementText input SQL statement or XQuery expression string.
SQLINTEGER TextLength input Number of SQLCHAR elements (or SQLWCHAR elements for the Unicode variant of this function) needed to store the StatementText argument, or SQL_NTS if StatementText is null-terminated.

Usage

If the SQL statement text contains vendor escape clause sequences, CLI will first modify the SQL statement text to the appropriate DB2-specific format before submitting it for preparation and execution. If the application does not generate SQL statements that contain vendor escape clause sequences, then it should set the SQL_ATTR_NOSCAN statement attribute to SQL_NOSCAN_ON at the connection level so that CLI does not perform a scan for vendor escape clauses.

The SQL statement can be COMMIT or ROLLBACK if it is called using SQLExecDirect(). Doing so yields the same result as calling SQLEndTran() on the current connection handle.

The SQL statement string can contain parameter markers, however all parameters must be bound before calling SQLExecDirect().

If the SQL statement is a query, or StatementText is an XQuery expression, SQLExecDirect() will generate a cursor name, and open the cursor. If the application has used SQLSetCursorName() to associate a cursor name with the statement handle, CLI associates the application generated cursor name with the internally generated one.

If a result set is generated, SQLFetch() or SQLFetchScroll() will retrieve the next row (or rows) of data into bound variables, LOB locators, or LOB file references.

If the SQL statement is a positioned DELETE or a positioned UPDATE, the cursor referenced by the statement must be positioned on a row and must be defined on a separate statement handle under the same connection handle.

There must not already be an open cursor on the statement handle.

If SQLSetStmtAttr() has been called with the SQL_ATTR_PARAMSET_SIZE attribute to specify that an array of input parameter values has been bound to each parameter marker, then the application needs to call SQLExecDirect() only once to process the entire array of input parameter values.

If the executed statement returns multiple result sets (one for each set of input parameters), then SQLMoreResults() should be used to advance to the next result set once processing on the current result set is complete.

Return codes

  • SQL_SUCCESS
  • SQL_SUCCESS_WITH_INFO
  • SQL_STILL_EXECUTING
  • SQL_ERROR
  • SQL_INVALID_HANDLE
  • SQL_NEED_DATA
  • SQL_NO_DATA_FOUND

SQL_NEED_DATA is returned when the application has requested to input data-at-execute parameter values by setting the *StrLen_or_IndPtr value specified during SQLBindParameter() to SQL_DATA_AT_EXEC for one or more parameters.

SQL_NO_DATA_FOUND is returned if the SQL statement is a Searched UPDATE or Searched DELETE and no rows satisfy the search condition.

Diagnostics

Table 2. SQLExecDirect SQLSTATEs
SQLSTATE Description Explanation
01504 The UPDATE or DELETE statement does not include a WHERE clause. StatementText contained an UPDATE or DELETE statement which did not contain a WHERE clause. (Function returns SQL_SUCCESS_WITH_INFO or SQL_NO_DATA_FOUND if there were no rows in the table).
01508 Statement disqualified for blocking. The statement was disqualified for blocking for reasons other than storage.
07001 Wrong number of parameters. The number of parameters bound to application variables using SQLBindParameter() was less than the number of parameter markers in the SQL statement contained in the argument StatementText.
07006 Invalid conversion. Transfer of data between CLI and the application variables would result in an incompatible data conversion.
21S01 Insert value list does not match column list. StatementText contained an INSERT statement and the number of values to be inserted did not match the degree of the derived table.
21S02 Degrees of derived table does not match column list. StatementText contained a CREATE VIEW statement and the number of names specified is not the same degree as the derived table defined by the query specification.
22001 String data right truncation. A character string assigned to a character type column exceeded the maximum length of the column.
22003 Numeric value out of range. A numeric value assigned to a numeric type column caused truncation of the whole part of the number, either at the time of assignment or in computing an intermediate result.

StatementText contained an SQL statement with an arithmetic expression which caused division by zero.

Note: as a result the cursor state is undefined for DB2® for Linux, UNIX, and Windows (the cursor will remain open for other RDBMSs).
22005 Error in assignment. StatementText contained an SQL statement with a parameter or literal and the value or LOB locator was incompatible with the data type of the associated table column.

The length associated with a parameter value (the contents of the pcbValue buffer specified on SQLBindParameter()) is not valid.

The argument fSQLType used in SQLBindParameter() or SQLSetParam(), denoted an SQL graphic data type, but the deferred length argument (pcbValue) contains an odd length value. The length value must be even for graphic data types.

22007 Invalid datetime format. StatementText contained an SQL statement with an invalid datetime format; that is, an invalid string representation or value was specified, or the value was an invalid date, time, or timestamp.
22008 Datetime field overflow. Datetime field overflow occurred; for example, an arithmetic operation on a date or timestamp has a result that is not within the valid range of dates, or a datetime value cannot be assigned to a bound variable because it is too small.
22012 Division by zero is invalid. StatementText contained an SQL statement with an arithmetic expression that caused division by zero.
23000 Integrity constraint violation. The execution of the SQL statement is not permitted because the execution would cause integrity constraint violation in the DBMS.
24000 Invalid cursor state. A cursor was already opened on the statement handle.
24504 The cursor identified in the UPDATE, DELETE, SET, or GET statement is not positioned on a row. Results were pending on the StatementHandle from a previous query or a cursor associated with the hstmt had not been closed.
34000 Invalid cursor name. StatementText contained a Positioned DELETE or a Positioned UPDATE and the cursor referenced by the statement being executed was not open.
37xxx a Invalid SQL syntax. StatementText contained one or more of the following:
  • an SQL statement that the connected database server could not prepare
  • a statement containing a syntax error
40001 Transaction rollback. The transaction to which this SQL statement belonged was rolled back due to a deadlock or timeout.
40003 08S01 Communication link failure. The communication link between the application and data source failed before the function completed.
42xxx Syntax Error or Access Rule Violation. 425xx indicates the authorization ID does not have permission to execute the SQL statement contained in StatementText.

Other 42xxx SQLSTATES indicate a variety of syntax or access problems with the statement.

428A1 Unable to access a file referenced by a host file variable. This can be raised for any of the following scenarios. The associated reason code in the text identifies the particular error:
  • 01 - The file name length is invalid, or the file name, the path has an invalid format, or both.
  • 02 - The file option is invalid. It must have one of the following values:
      SQL_FILE_READ      -read from an existing file
      SQL_FILE_CREATE    -create a new file for write
      SQL_FILE_OVERWRITE -overwrite an existing file.
                          If the file does not exist,
                          create the file.
      SQL_FILE_APPEND    -append to an existing file.
                          If the file does not exist,
                          create the file.
  • 03 - The file cannot be found.
  • 04 - The SQL_FILE_CREATE option was specified for a file with the same name as an existing file.
  • 05 - Access to the file was denied. The user does not have permission to open the file.
  • 06 - Access to the file was denied. The file is in use with incompatible modes. Files to be written to are opened in exclusive mode.
  • 07 - Disk full was encountered while writing to the file.
  • 08 - Unexpected end of file encountered while reading from the file.
  • 09 - A media error was encountered while accessing the file.
42895 The value of a host variable in the EXECUTE or OPEN statement cannot be used because of its data type. The LOB locator type specified on the bind parameter function call does not match the LOB data type of the parameter marker.

The argument fSQLType used on the bind parameter function specified a LOB locator type but the corresponding parameter marker is not a LOB.

44000 Integrity constraint violation. StatementText contained an SQL statement which contained a parameter or literal. This parameter value was NULL for a column defined as NOT NULL in the associated table column, or a duplicate value was supplied for a column constrained to contain only unique values, or some other integrity constraint was violated.
56084 LOB data is not supported in DRDA®. LOB columns cannot either be selected or updated when connecting to host or AS/400® servers (using DB2 Connect™).
58004 Unexpected system failure. Unrecoverable system error.
S0001 Database object already exists. StatementText contained a CREATE TABLE or CREATE VIEW statement and the table name or view name specified already existed.
S0002 Database object does not exist. StatementText contained an SQL statement that references a table name or view name which does not exist.
S0011 Index already exists. StatementText contained a CREATE INDEX statement and the specified index name already existed.
S0012 Index not found. StatementText contained a DROP INDEX statement and the specified index name did not exist.
S0021 Column already exists. StatementText contained an ALTER TABLE statement and the column specified in the ADD clause was not unique or identified an existing column in the base table.
S0022 Column not found. StatementText contained an SQL statement that references a column name which does not exist.
HY001 Memory allocation failure. DB2 CLI is unable to allocate memory required to support execution or completion of the function. It is likely that process-level memory has been exhausted for the application process. Consult the operating system configuration for information on process-level memory limitations.
HY009 Invalid argument value. StatementText was a null pointer.
HY013 Unexpected memory handling error. DB2 CLI was unable to access memory required to support execution or completion of the function.
HY014 No more handles. DB2 CLI was unable to allocate a handle due to resource limitations.
HY090 Invalid string or buffer length. The argument TextLength was less than 1 but not equal to SQL_NTS.
HY092 Option type out of range. The FileOptions argument of a previous SQLBindFileToParam() operation was not valid.
HY503 Invalid file name length. The fileNameLength argument value from SQLBindFileToParam() was less than 0, but not equal to SQL_NTS.
HYT00 Timeout expired. The timeout period expired before the data source returned the result set. The timeout period can be set using the SQL_ATTR_QUERY_TIMEOUT attribute for SQLSetStmtAttr().
Note:
a
xxx refers to any SQLSTATE with that class code. Example, 37xxx refers to any SQLSTATE in the 37 class.

Restrictions

None.

Example

  /* directly execute a statement - end the COMPOUND statement */
  cliRC = SQLExecDirect(hstmt, (SQLCHAR *)"SELECT * FROM ORG", SQL_NTS);