SAP EXEC CURSOR ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• OPEN FOR SELECT EXEC SQL

ABAP_EXEC_SQL - OPEN, FETCH, CLOSE
In embedded Native SQL, similar statements to those in AB_SQL can be specified to read data using a database cursor.

ABAP_SYNTAX
EXEC SQL.
OPEN dbcur FOR SELECT ...
ENDEXEC.

What does it do?
Opens a database cursor dbcur. For dbcur, a flat character-like host variable can be specified.



Latest notes:

The number of database cursors open simultaneously is restricted by the platform. Any attempts to open too many database cursors raise an exception of the class CX_SY_NATIVE_SQL_ERROR.
ABAP_HINT_END
• FETCH EXEC SQL

ABAP_SYNTAX
EXEC SQL.
FETCH NEXT dbcur INTO ...
ENDEXEC.

What does it do?
Uses an open database cursor dbcur to read data to the host variables specified after INTO.
• CLOSE EXEC SQL

ABAP_SYNTAX
EXEC SQL.
CLOSE dbcur
ENDEXEC.

What does it do?
Closes an opened database cursor dbcur.
If no row can be read using FETCH, sy-subrc is set to 4 by ENDEXEC. After a FETCH statement, the system field sy-dbcnt is set to the number of rows read up to that point using the relevant cursor. If an overflow occurs because the number or rows is greater than 2,147,483,647, sy-dbcnt is set to -1.



Latest notes:

It depends on the database system whether the database cursor in the database is closed implicitly after the extraction of the final row of the result set or not. For this reason, it is advisable to use the statement CLOSE dbcur explicitly.
ABAP_HINT_END



Example ABAP Coding

Reading of multiple rows from the database table SPFLI using cursor handling and host variables in static Native SQL. If rows are found, sy-subrc is set to 0 and sy-dbcnt is increased by one for each row read.
ABEXA 00241
ABAP_EXAMPLE_END

Return to menu