Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Open Cursor Vs Open Sql (SELECT)

0 Likes
4,719

Between "OPEN CURSOR" and Open SQL (SELECT)

If I using open cursor with parallel and Nornal Select statement, Which one is better than?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,353

Hi Veera,

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.

And the Open SQL which is a normal Select statement.

kindly reward if found helpful.

cheers,

Hema.

3 REPLIES 3
Read only

Former Member
0 Likes
2,354

Hi Veera,

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.

And the Open SQL which is a normal Select statement.

kindly reward if found helpful.

cheers,

Hema.

Read only

Former Member
0 Likes
2,353

Hi Veera, check this Dynamic SQL statement also.

Following is the code which i used for a dynamic SQL Query and it worked for me.

DATA where_tab(25) OCCURS 5 WITH HEADER LINE.

DATA p_kunnar like vbak-kunnr.

CONDENSE p_kunnar NO-GAPS.

IF p_kunnar NE ''.

APPEND 'vbak~kunnr = p_kunnar' TO where_tab.

ENDIF.

SELECT DISTINCT vbeln

FROM vbak

INTO CORRESPONDING FIELDS OF TABLE gi_vtab

WHERE vbak-vkorg = p_vkorg AND

vbak-vtweg = p_vtweg AND

vbak-auart = 'LP' AND

(where_tab).

cheers,

Hema.

Read only

Former Member
0 Likes
2,353

hi,

An OPEN CURSOR statement generates the result table defined under the specified name with a DECLARE CURSOR statement.

<open_cursor_statement> ::= OPEN <result_table_name>

The DECLARE CURSOR statement defines a named result table (see named/unnamed result table) with the name result_table_name .

<declare_cursor_statement> ::= DECLARE <result_table_name> CURSOR FOR <select_statement>

A SELECT statement (select_statement) defines and creates an unnamed result table (see named/unnamed result table).

Syntax

<select_statement> ::= <query_expression> [<order_clause>] [<update_clause>] [<lock_option>] [FOR REUSE]

query expression, order clause, update clause, lock option

An OPEN CURSOR statement is not permitted for result tables created with this SELECT statement.

The SELECT statement (select_statement) is subject to the rules that were specified for the DECLARE CURSOR statement and those that were specified for the OPEN CURSOR statement.

Depending on the search strategy, either all the rows in the result table are searched when the SELECT statement (select_statement) is executed and the result table is physically generated, or each next result table row is searched when a FETCH statement is executed, without being physically stored. This must be taken into account for the time behavior of FETCH statements.

Open SQL consists of a set of ABAP statements that perform operations on the central database in the SAP Web AS ABAP. The results of the operations and any error messages are independent of the database system in use. Open SQL thus provides a uniform syntax and semantics for all of the database systems supported by SAP. ABAP programs that only use Open SQL statements will work in any SAP system, regardless of the database system in use. Open SQL statements can only work with database tables that have been created in the ABAP Dictionary.

open sql

In the ABAP Dictionary, you can combine columns of different database tables to a database view (or view for short). In Open SQL statements, views are handled in exactly the same way as database tables. Any references to database tables in the following sections can equally apply to views.

The Open SQL statement for reading data from database tables is:

SELECT result

INTO target

FROM source

[WHERE condition]

[GROUP BY fields]

[HAVING cond]

[ORDER BY fields].

The SELECT statement is divided into a series of simple clauses, each of which has a different part to play in selecting, placing, and arranging the data from the database.

pls reward points if helpful,

shylaja