Difference between CURSOR and SELECT statement

by | Jun 23, 2020 | ABAP Differences

Home » SAP » ABAP » ABAP Differences » Difference between CURSOR and SELECT statement

Preface – This post is part of the Differences in ABAP for Interviews series.

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.

  1. 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.

  1. 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.

  1. 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 statementSELECT 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 serverData is directly copied into a data object in the ABAP memory

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author