SAP OPEN CURSOR ABAP Statements
Get Example source ABAP code based on a different SAP table
ID OPEN-CURSOR
• OPEN CURSOR ABAP_STATEMENT_OSQL
• FOR SELECT OPEN CURSOR
OPEN CURSOR> Short Reference >
ABAP_SYNTAX
OPEN CURSOR $[WITH HOLD$] @dbcur$|@DATA(dbcur) FOR
$[WITH>
+cte1 AS ( SELECT subquery_clauses> )$[,
+cte2 AS ( SELECT subquery_clauses> )
...$]$]
SELECT mainquery_clauses>
$[UNION$|INTERSECT$|EXCEPT ...>$]
$[abap_options>$].>
ABAP Addition
... WITH HOLD>
What does it do?
The AB-SQL > statement OPEN CURSOR> opens a database cursor> for the result set of the main query> defined after FOR> and links a cursor variable dbcur> with this database cursor. The result set of the main query can be read with the statement FETCH>>.
The main query is specified after FOR> as follows:
The main query is specified using the language element SELECT>, and its clauses and additions mainquery_clauses>> define the result set.
The set operator> UNION>>, INTERSECT>>, or EXCEPT>> can be used to combine the result sets of multiple queries. In this case, special rules query_clauses>> apply when specifying clauses.
Finally, the optional ABAP-specific additions abap_options> > can be specified.
Optional common table expressions> can be defined in the main query using the language element WITH>. When defining and using common table expressions, the same applies as when using WITH> to introduce a standalone statement>.
The following can be specified for the cursor:
A host variable> dbcur> declared with the special predefined data type cursor>>. A database cursor dbcur> that has already been opened cannot be opened again.
An inline declaration> of a corresponding host variable> dbcur>. Only the declaration operator> DATA>> is possible and must be prefixed with the escape character @>. The declaration operator FINAL>> is not possible here.
A row of the result set is always assigned to an opened database cursor as a cursor position. After the statement OPEN CURSOR>, the database cursor is positioned in front of the first row of the result set.
In a program, a maximum of 17 database cursors can be open simultaneously across the AB-SQL interface>. If an attempt is made to open more than 17 database cursors, the runtime error DBSQL_TOO_MANY_OPEN_CURSOR> occurs. An open database cursor can be closed using the statement CLOSE CURSOR>>. In addition, any open database cursors are closed by database commits> or database rollbacks> if these occur after the first use of the cursor in a FETCH>> statement.
If a cursor variable dbcur> of an open database cursor is assigned to another cursor variable or passed as a parameter, the latter is linked with the same database cursor at the same position. A cursor variable of an open database cursor can also be passed to externally called procedures> to access the database cursor from there.
Latest notes:
It is not recommended that cursor variables are assigned to each other and they should be set only using the statements OPEN CURSOR> and CLOSE CURSOR>.
If write accesses> are made on a DDIC database table for which a database cursor is open, the result set is database-dependent and undefined. This kind of parallel access should therefore be avoided.
As well as explicit AB-SQL reads using OPEN CURSOR> and SELECT>> loops, the AB-SQL interface also opens database cursors implicitly, such as when loading buffered> tables. The runtime error DBSQL_TOO_MANY_OPEN_CURSOR> can be avoided by not using explicit reads to exploit the maximum number of open database cursors.
If a CDS view is defined as a replacement object> for a DDIC database table or DDIC database view specified as a data source> of the SELECT> statement of OPEN CURSOR>, the statement FETCH>> accesses the CDS view and not the DDIC database table or the DDIC database view.
Host variables without the escape character @> are obsolete>. The escape character @> must be specified in the strict modes> of the syntax check from ABAP_RELEASE ABAP_740_SP05 . The use of an inline declaration for dbcur> activates the strict mode with ABAP_STRICT_765 release ABAP_765 / and higher. If a statement OPEN CURSOR> is checked in accordance with the rules for the ABAP_STRICT_760 strict mode / from ABAP_RELEASE ABAP_760 , each statement FETCH>> that accesses the database cursor is also checked in strict mode. Conversely, the strict syntax check mode from ABAP_STRICT_777 ABAP_RELEASE ABAP_777 / also applies the strict mode for the associated statement OPEN CURSOR> in the statement FETCH >. A violation of the rules specified by FETCH> in OPEN CURSOR> raises an exception of the class CX_SY_DYNAMIC_OSQL_SEMANTICS>.
ABAP_HINT_END
Example ABAP Coding
Opens two cursors for the DDIC database table SPFLI>. For more information on how to use this function, see the example for FETCH>>.
ABEXA 00456
ABAP_EXAMPLE_END
• WITH HOLD OPEN CURSOR
ABAP Addition
What does it do?
If the addition WITH HOLD> is specified, the database cursor is not closed in a database commit> executed using Native SQL>.
The addition WITH HOLD> can be used only in reads performed on the standard database. It cannot be specified together with the addition CONNECTION>>.
Latest notes:
A Native SQL database commit closes the database cursor only after the cursor is used in a FETCH> > statement. A Native SQL database commit between the statement OPEN CURSOR> and the first FETCH> statement does not close the cursor.
The addition WITH HOLD> is ignored by the following:
Implicit database commits>
Commits made by the statement COMMIT WORK>>
Any rollbacks These always close the database cursor.
A Native SQL database commit can be made explicitly using the statement COMMIT CONNECTION> >.
BEGIN_SECTION SAP_INTERNAL_HINT
Restriction for LOB Handles not yet relevant.
END_SECTION SAP_INTERNAL_HINT
ABAP_HINT_END
Example ABAP Coding
The addition WITH HOLD> prevents the database cursor from being closed using an explicit database commit with the statement COMMIT CONNECTION>> and hence prevents an exception from being raised in the second FETCH>> statement. An exception is, however, raised after the statement COMMIT WORK>.
ABEXA 00457
ABAP_EXAMPLE_END
Return to menu