Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

open cursor???

Hi all ,

Wat is the use of statement open cursor & how it should be used.


Former Member
Former Member replied



SELECT result

FROM source

[[FOR ALL ENTRIES IN itab] WHERE sql_cond]

[GROUP BY group] [HAVING group_cond]

[ORDER BY sort_key].

Addition: ... WITH HOLD

This statement opens a database cursor for the selection defined after FOR, and links a cursor variable dbcur with this database cursor. For dbcur, a declared variable with the specific predefined data type cursor must be entered. A database cursor dbcur that has already been opened cannot be opened again. A line of the resulting set is always assigned to an opened database cursor as a cursor position. After the OPEN CURSOR statement, the database cursor is positioned in front of the first line of the resulting set.

After FOR, the syntax of a SELECT statement can be entered, which contains all the additions of the normal SELECT statement, except for INTO and APPENDING. In the addition result, the addition SINGLE can also not be used after SELECT.

Only a limited number of database cursors can be open at the same time. An open database cursor can be closed using the statement CLOSE CURSOR . In addition, an open database cursor is closed for a database commit or a database rollback.

If a cursor variable dbcur of an open database cursor is assigned to another cursor variable, the latter is linked to the same database cursor at the same position. A cursor variable of an open database cursor can also be transferred to procedures that have been called externally, to enable the database cursor to be accessed from there.

It is not recommended to assign cursor variables to each other, but rather to set them exclusively using the statements OPEN CURSOR and CLOSE CURSOR.

Addition ... WITH HOLD


If the addition WITH HOLD is specified, the database cursor is not closed in an explicitly triggered database commit or database rollback, for example Native SQL. The addition WITH HOLD cannot be specified if the cursor is to be opened for a secondary database connection.



This statement extracts the requested rows (by use of the addition INTO resp. APPENDING) from the resulting set of the database cursor (which is linked to the cursor variable dbcur) from the current cursor-position and assigns these rows to the data objects specified in the resulting set.

The cursor-variable dbcur has to be a variable declared by the special pre-defined data type cursor, which was opened with the statement OPEN CURSOR, or which had an opened cursor assigned to.

Syntax and meaning of the addition INTO resp. APPENDING target are completely synonymous to the additions of the SELECT-statement. If you specify non-table-type data objects after INTO, then one line is extracted. If an internal table is specified after INTO resp. APPENDING, then, either all lines get extracted ,or as many as specified in the addition PACKAGE SIZE.

The statement FETCH moves the position of the database cursor (which is linked to dbcur) by the amount of extracted lines to the next line to be extracted. If you extracted the last line of the resulting set in a FETCH-statement, then every following FETCH-Statement, in which dbcur is linked to the same database-cursor, sets sy-subrc to 4, without influencing the data objects specified after INTO resp. APPENDING.

System Fields

The statement FETCH sets the values of the system fields sy-subrc and sy-dbcnt.

sy-subrc description

0 At least one line was extracted from the resulting set.

4 No line was extracted.

The statement FETCH sets sy-dbcnt after every line extraction to the amount of the lines that have been extracted so far from the resulting set in concern. If no line can be extracted, then sy-dbcnt is set to 0.

Subsequent FETCH-statements, which access the same resulting set, can have different additions INTO res. APPENDING: The specification of work areas can be combined with the specification of internal tables. In doing so, the addition CORRESPONDING FIELDS is either not listed at all in all the FETCH-statements, or has to be listed in every statement. Moreover, the data types of all involved work areas wa resp. the line types of the internal tables itab have to be identical. The specification of a bracketed list of data objects after INTO can not be combined with the specification of work areas or internal tables, but every involved FETCH-statement has to contain such a list.


Reading of data of the database table SPFLI in pakets of varying size with the help of two parallel cursors. The paket size is determined by the first cursor through the aggregation functioncount( * ) and used via the second cursor when accessing. The variable control of the addition PACKAGE SIZE is not possible within a single SELECT-statement.

DATA: BEGIN OF count_line,

carrid TYPE spfli-carrid,

count TYPE i,

END OF count_line,

spfli_tab TYPE TABLE OF spfli.

DATA: dbcur1 TYPE cursor,

dbcur2 TYPE cursor.


SELECT carrid count(*) AS count

FROM spfli

GROUP BY carrid

ORDER BY carrid.



FROM spfli

ORDER BY carrid.


FETCH NEXT CURSOR dbcur1 INTO count_line.

IF sy-subrc <> 0.




INTO TABLE spfli_tab PACKAGE SIZE count_line-count.





If the result set consists of multiple lines, an internal table itab of any table type can be specified after INTO or APPENDING. The row type of the internal table must meet the prerequisites.

The result set is inserted into the internal table itab line-by-line; a sorting process is executed in the case of a sorted table. If INTO is used, the internal table is initialized before the first line is inserted. Previous lines remain intact if APPENDING is used.

Before any assignment of a line of the result set, an initial row of the internal table itab is created and the line of the result set is assigned to this row. When assigning a line of the result set to a row of the internal table with or without CORRESPONDING FIELDS, the same rules apply as when assigning to an individual work area wa (see above).

If the PACKAGE SIZE addition is not used, all lines of the result set are inserted in the internal table itab and the ENDSELECT statement must not be specified after SELECT.

If you specify the PACKAGE SIZE addition, all lines of the result set are processed in a loop, which must be closed with ENDSELECT. The lines are inserted in the internal table itab in packages of n lines. n must be a type i data object that contains the number of lines. If the value of n is smaller than 0, an exception that cannot be handled occurs. If n is equal to 0, all lines of the result set are inserted in the internal table itab.

If INTO is used, the internal table is initialized before each loop pass and, in the SELECT loop, it only contains the lines of thecurrent package. If APPENDING is used, a further package is added to the existing rows of the internal table in each SELECT loop.

After ENDSELECT, the contents of itab is not defined if INTO is used - that is, the table can either contain the lines of the last package or it can be initial. If APPENDING is used, the content of itab retains the state of the last loop pass.

In the case of an internal table with a unique key, an exception that cannot be handled occurs if an attempt is made to create a duplicate entry


In this example, all columns of the result set are read into an internal table whose row type is a nested structure with the same construction as the result set. Note that in practice, the column carrid exists twice in the result set with the same content and, after the assignment, this content is stored redundantly in the columns struc1-carrid and struc2-carrid of the internal table.


struc1 TYPE scarr,

struc2 TYPE spfli,

END OF wa.


WITH UNIQUE KEY table_line.


FROM scarr

INNER JOIN spfli ON scarrcarrid = spflicarrid


LOOP AT itab INTO wa.

WRITE: / wa-struc1-carrid,





0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question