ABAP Keyword Documentation → ABAP - Reference → Processing External Data → ABAP Database Accesses → Native SQL → ADBC - ABAP Database Connectivity → CL_SQL_STATEMENT - Execution of SQL Statements
Queries
Queries can be executed using the following instance method from class CL_SQL_STATEMENT:
- EXECUTE_QUERY
The method has a mandatory input parameter STATEMENT of type string
that must be passed to a SELECT
statement with correct syntax. As with
DML statements, the method SET_PARAM can be used to bind ABAP data objects to place holders ?
.
As the result of a query, a reference to an object of the class CL_SQL_RESULT_SET is returned in the return value RESULT_SET. The methods of this object allow access to the results set of the query. To retain the results set beyond the end of a database LUW, the input parameter HOLD_CURSOR of the EXECUTE_QUERY method can be filled with "X".
The class CL_SQL_RESULT_SET of the result object provides the following instance methods for reading the results sets into ABAP data objects:
- SET_PARAM, NEXT, and CLOSE
These methods provide access to individual rows and columns of the results set. Using SET_PARAM, compatible ABAP data objects must be assigned to the columns from left to right by passing corresponding data references for each column to the method. NEXT is used to address the rows of the results set one after another. The return value is 1 if the row can be addressed and 0 if not. Reads are closed using CLOSE. If the parameter binding between two calls of NEXT is to be modified, the method CLEAR_PARAMETERS must be called first.
- SET_PARAM_STRUCT, NEXT, and CLOSE
These methods provide access to individual rows of the results set. SET_PARAM_STRUCT must be used to assign a fully compatible ABAP structure to the rows of the results set by passing corresponding data references to the method. An internal table that specifies the names and order of the columns to be read can be passed to the parameter CORRESPONDING_FIELDS. For the remaining methods, the same applies as to SET_PARAM.
- SET_PARAM_TABLE, NEXT_PACKAGE, and CLOSE
These methods provide access to multiple rows of the results set. SET_PARAM_TABLE must be used to assign a fully compatibly structured standard table to the rows of the results set by passing a corresponding data reference to the method. As with SET_PARAM_STRUCT, a CORRESPONDING_FIELDS parameter is used to specify which columns are to be transported. Here, NEXT_PACKAGE is used instead of NEXT. It reads at most the number of rows that are passed to the input parameter UPTO. If no value is passed to UPTO, all the rows are read. In each call of NEXT_PACKAGE, the rows read are appended to the internal table without deleting the previous contents and the number of rows read is returned in the return value ROWS_RET. The same applies when changing parameter bindings and to CLOSE as to SET_PARAM.
Other versions: 7.31 | 7.40 | 7.54
Notes
- A data reference to an indicator variable with predefined type INT2 from ABAP Dictionary can be passed to the optional input parameter IND_REF of the method SET_PARAM. Here, the value -1 can be used to display whether a zero value existed on the database.
- For security reasons, it is better to define the parameters of a query using the placeholder
?
rather than chaining dynamic content. This is also a way of preventing SQL injections. If the statement only contains static content from the program and dynamic content from outside the program is possible only in operand positions (using placeholders), the statement cannot be modified from outside.
Examples
See