Preface – This post is part of the Differences in ABAP for Interviews series.
Table of Contents
Introduction
Before discussing the difference between CURSOR and SELECT statement, we will have a short introduction of the two. Both of these operations are used in SAP ABAP to perform operations on SAP Tables/Data.
CURSOR
CURSOR decouples the process of SELECT statement and INTO clause. This is done by opening a cursor for a select statement. This is used to extract data in chunks from the database table. Only after getting the complete data, the data is stored in the target area
It follows the following three-stage process to access the data from the database.
Opening the cursor
Syntax to open the cursor for SELECT statement:
OPEN CURSOR [WITH HOLD] <c> FOR SELECT <result>
FROM <source>
[WHERE <condition>] [GROUP BY <fields>] [HAVING <cond>] [ORDER BY <fields>].
To open a cursor, it is required to declare it first. It is declared using the DATA statement and special data type CURSOR. All clauses of SELECT statement can be used except the INTO clause.
After the OPEN CURSOR statement, the database cursor points to the first line of the result set. The open cursor points to an internal handler just like a reference variable pointing to an object. More than one cursor can be opened parallelly for a single database.
Fetching the data
Syntax to read data :
FETCH NEXT CURSOR <c> INTO <target>.
This statement decouples the INTO clause from all other clauses in the SELECT statement. This extracts the requested row from the database cursor, assigns into the target area and the cursor moves one line further in the selection set.
Closing the cursor
Syntax to close the cursor:
CLOSE CURSOR <c>.
If a cursor is already opened, it cannot be reopened. It is necessary to close all the cursors which are no longer required since only a limited number of cursors can be opened simultaneously.
SELECT statement
SELECT statement is an Open SQL statement for reading data from one or more database tables, classic views, or CDS views. The SELECT statement reads the data directly into the target area specified in the INTO clause. While using SELECT…END SELECT statement, database is accessed each and every time fetching the data row by row. A SELECT statement makes use of the CURSORS implicitly to fetch the data from the database.
Difference between CURSOR and SELECT statement
Now, let’s have a look at their difference.
CURSOR statement | SELECT statement |
This decouples the process of SELECT statement and INTO clause. Only after getting the complete data, the data is stored in the target area. | The data is stored directly into the target area specified in the INTO clause. |
This creates a pointer to database record and data is not copied to the ABAP server | Data is directly copied into a data object in the ABAP memory |
0 Comments