AS ABAP Release 753, ©Copyright 2019 SAP AG. All rights reserved.
ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP Database Access → ABAP SQL → ABAP SQL - Reads →
OPEN CURSOR
Syntax
OPEN CURSOR [WITH HOLD] @dbcur|@DATA(dbcur) FOR
[WITH
+cte1 AS ( SELECT
subquery_clauses )[,
+cte2 AS ( SELECT subquery_clauses )
...]]
SELECT mainquery_clauses
[UNION ...]
[UP TO ...] [OFFSET ...]
[abap_options].
Addition:
Effect
The ABAP 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 results set of the main query can be read with the statement FETCH.
The main query is specified after FOR as follows:
The following can be specified for the cursor:
A line 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 line of the results set.
In a single program, a maximum of 17 database cursors can be open simultaneously across the ABAP SQL interface. If more than 17 database cursors are opened, the runtime error DBSQL_TOO_MANY_OPEN_CURSOR occurs. An open database cursor can be closed using the statement CLOSE CURSOR. Here, 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 associated with the same database cursor at the same position. A cursor variable of an open database cursor can also be passed to procedures that have been called externally, to enable the database cursor to be accessed from there.
Notes
Example
Opens two cursors for the database table SPFLI. For more information on how to use this function, see the example for FETCH.
OPEN CURSOR @DATA(dbcur1) FOR
SELECT carrid, COUNT(*) AS count
FROM spfli
GROUP BY carrid
ORDER BY carrid.
OPEN CURSOR @DATA(dbcur2) FOR
SELECT *
FROM spfli
ORDER BY carrid.
... WITH HOLD
Effect
If the addition WITH HOLD is specified, the database cursor is not closed by 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.
Notes
Example
The addition WITH HOLD stops the database cursor from being closed using an explicit database commit and the statement COMMIT CONNECTION and hence stops an exception from being raised in the second FETCH statement. An exception is, however, raised after the statement COMMIT WORK.
DATA wa TYPE scarr.
OPEN CURSOR WITH HOLD @DATA(dbcur) FOR
SELECT *
FROM scarr.
FETCH NEXT CURSOR @dbcur INTO @wa.
COMMIT CONNECTION default.
FETCH NEXT CURSOR @dbcur INTO @wa.
CLOSE CURSOR @dbcur.
TRY.
OPEN CURSOR WITH HOLD @dbcur FOR
SELECT *
FROM scarr.
COMMIT WORK.
FETCH NEXT CURSOR @dbcur INTO @wa.
CLOSE CURSOR @dbcur.
CATCH cx_sy_open_sql_db.
...
ENDTRY.