Skip to Content

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

Open cursor

what is the use of open cursor?

Tags:
Former Member
Former Member replied

Hi

OPEN CURSOR

OPEN CURSOR [WITH HOLD] dbcur FOR

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.

*****************************************************

Using a Cursor to Read Data

In the normal SELECT statement, the data from the selection is always read directly into the target area specified in the INTO clause during the SELECT statement. When you use a cursor to read data, you decouple the process from the SELECT statement. To do this, you must open a cursor for a SELECT statement. Afterwards, you can place the lines from the selection into a flat target area.

Opening and Closing Cursors

To open a cursor for a SELECT statement, use the following:

OPEN CURSOR [WITH HOLD] <c> FOR SELECT <result>

FROM <source>

[WHERE <condition>]

[GROUP BY <fields>]

[HAVING <cond>]

[ORDER BY <fields>].

You must first have declared the cursor <c> using the DATA statement and the special data type CURSOR. You can use all clauses of the SELECT statement apart from the INTO clause. Furthermore, you can only formulate the SELECT clause so that the selection consists of more than one line. This means that you may not use the SINGLE addition, and that the column selection may not contain only aggregate expressions.

An open cursor points to an internal handler, similarly to a reference variable pointing to an object. You can reassign cursors so that more than one points to the same handler. In a MOVE statement, the target cursor adopts all of the attributes of the source cursor, namely its position, and all of the clauses in the OPEN CURSOR statement.

You can also open more than one cursor in parallel for a single database table. If a cursor is already open, you cannot reopen it. To close a cursor explicitly, use the following statement:

CLOSE CURSOR <c>.

You should use this statement to close all cursors that you no longer require, since only a limited number of cursors may be open simultaneously. With one exception, a database LUW is concluded when you close a cursor either explicitly or implicitly. The WITH HOLD addition in the OPEN CURSOR statement allows you to prevent a cursor from being closed when a database commit occurs in Native SQL.

Reading Data

An open cursor is linked to a multiple-line selection in the database table. To read the data into a target area in the ABAP program, use the following:

FETCH NEXT CURSOR <c> INTO <target>.

This writes one line of the selection into the target area <target>, and the cursor moves one line further in the selection set. The fetch statement decouples the INTO clause from the other clauses in the SELECT statement. All the INTO clauses of the SELECT statement can be used. The statement reads the lines that are needed to fill the target area of the INTO clause and moves the cursor to the next line to be read.

SY-SUBRC is set to 0 until all the lines of the selection have been read; otherwise it is 4. After a FETCH statement, system field SY-DBCNT contains the number of all the lines read so far for the corresponding cursor.

REPORT demo_select_cursor_1.

DATA: c1 TYPE cursor,

c2 TYPE cursor.

DATA: wa1 TYPE spfli,

wa2 TYPE spfli.

DATA: flag1(1) TYPE c,

flag2(1) TYPE c.

OPEN CURSOR: c1 FOR SELECT carrid connid

FROM spfli

WHERE carrid = 'LH',

c2 FOR SELECT carrid connid cityfrom cityto

FROM spfli

WHERE carrid = 'AZ'.

DO.

IF flag1 NE 'X'.

FETCH NEXT CURSOR c1 INTO CORRESPONDING FIELDS OF wa1.

IF sy-subrc <> 0.

CLOSE CURSOR c1.

flag1 = 'X'.

ELSE.

WRITE: / wa1-carrid, wa1-connid.

ENDIF.

ENDIF.

IF flag2 NE 'X'.

FETCH NEXT CURSOR c2 INTO CORRESPONDING FIELDS OF wa2.

IF sy-subrc <> 0.

CLOSE CURSOR c2.

flag2 = 'X'.

ELSE.

WRITE: / wa2-carrid, wa2-connid,

wa2-cityfrom, wa2-cityto.

ENDIF.

ENDIF.

IF flag1 = 'X' AND flag2 = 'X'.

EXIT.

ENDIF.

ENDDO.

The output looks something like this:

The database table SPFLI is read using two cursors, each with different conditions.. The selected lines are read alternately in a DO loop.

REPORT demo_select_cursor_2.

DATA c TYPE cursor.

DATA wa TYPE sbook.

OPEN CURSOR c FOR SELECT carrid connid fldate bookid smoker

FROM sbook

ORDER BY carrid connid fldate smoker bookid.

FETCH NEXT CURSOR c INTO CORRESPONDING FIELDS OF wa.

WHILE sy-subrc = 0.

IF wa-smoker = ' '.

PERFORM nonsmoker USING c.

ELSEIF wa-smoker = 'X'.

PERFORM smoker USING c.

SKIP.

ELSE.

EXIT.

ENDIF.

ENDWHILE.

FORM nonsmoker USING n_cur TYPE cursor.

WHILE wa-smoker = ' ' AND sy-subrc = 0.

FORMAT COLOR = 5.

WRITE: / wa-carrid, wa-connid, wa-fldate, wa-bookid.

FETCH NEXT CURSOR n_cur INTO CORRESPONDING FIELDS OF wa.

ENDWHILE.

ENDFORM.

FORM smoker USING s_cur TYPE cursor.

WHILE wa-smoker = 'X' AND sy-subrc = 0.

FORMAT COLOR = 6.

WRITE: / wa-carrid, wa-connid, wa-fldate, wa-bookid.

FETCH NEXT CURSOR s_cur INTO CORRESPONDING FIELDS OF wa.

ENDWHILE.

ENDFORM.

The following is an extract from the list display:

The program opens a cursor for the database table SBOOK. After the first FETCH statement, a subroutine is called, which is dependent on the contents of the SMOKER column. The cursor is passed to an interface parameter in the subroutine. The subroutines read further lines until the contents of the SMOKER column change. The subroutines perform different tasks using the lines read by the cursor.

REPORT demo_select_cursor_3.

DATA: wa_spfli TYPE spfli,

wa_sflight TYPE sflight,

wa_sflight_back TYPE sflight.

DATA: c1 TYPE cursor,

c2 TYPE cursor.

OPEN CURSOR c1 FOR SELECT *

FROM spfli

ORDER BY PRIMARY KEY.

OPEN CURSOR c2 FOR SELECT *

FROM sflight

ORDER BY PRIMARY KEY.

DO.

FETCH NEXT CURSOR c1 INTO wa_spfli.

IF sy-subrc NE 0.

EXIT.

ENDIF.

WRITE: / wa_spfli-carrid, wa_spfli-connid.

DO.

IF NOT wa_sflight_back IS INITIAL.

wa_sflight = wa_sflight_back.

CLEAR wa_sflight_back.

ELSE.

FETCH NEXT CURSOR c2 INTO wa_sflight.

IF sy-subrc <> 0.

EXIT.

ELSEIF wa_sflight-carrid <> wa_spfli-carrid

OR wa_sflight-connid <> wa_spfli-connid.

wa_sflight_back = wa_sflight.

EXIT.

ENDIF.

ENDIF.

WRITE: / wa_sflight-carrid, wa_sflight-connid,

wa_sflight-fldate.

ENDDO.

ENDDO.

The output is as follows:

The program opens a cursor for each of the table SPFLI and SFLIGHT. Since both tables are linked by a foreign key relationship, it is possible to program a nested loop by sorting the selection by its primary key, so that the data read in the inner loop depends on the data in the outer loop. This programming method is quicker than using nested SELECT statements, since the cursor for the inner loop does not continually have to be reopened. If the group level in the inner loop is changed, the data that is read is stored temporarily until the next loop pass since it is not possible to reset the cursor.

Regards,

Sree

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