ABAP SELECT statement basics

  SQL

OverView

Here I would like to touch upon the basics of the SQL SELECT clause.
First of all, please refer to the sample code.

Sample Code

DATA: wa_mara TYPE mara,
      it_mara TYPE TABLE OF mara.

*1. Get only the first one
SELECT * FROM mara UP TO 1 ROWS
  INTO wa_mara.
ENDSELECT.

*2. Get only the first one
SELECT SINGLE * FROM mara
  INTO wa_mara.

*3. Acquire all while processing one by one
SELECT * FROM mara INTO wa_mara.
  "Process
  "IF XXXXXXX
ENDSELECT.

*4. Get all records at once
SELECT * FROM mara INTO TABLE it_mara.

*5. Get multiple items
SELECT SINGLE matnr mtart FROM mara
  INTO (wa_mara-matnr,wa_mara-mtart).

Explanation

1. gets multiple records, but when you get 1 record by UP TO 1 ROWS option
SQL ends. Therefore, this is the SQL used when you want to retrieve only one record.

2. is the SQL to get only one record. For the difference between the SINGLE option and the UP TO 1 ROWS option
There are various discussions, but when searching by specifying the primary key, use the SINGLE option, otherwise use the UP TO 1 ROWS option.
The personal view is to use it.

3. is used when one record is acquired and then some processing is performed and the next processing is performed.
However, it is effective when the number of records is small, but performance is deteriorated when the number of records is tens of thousands, so it is not recommended to use it.

4. is SQL that stores the fetched records in an internal table. I think it will be used most.

5. is the SQL when specifying multiple items to be acquired. When fetching multiple items, parentheses () are required after the INTO clause.