HOW TO USE OPEN-SQL SELECT STATEMENTS IN SAP ABAP

The data from the Database table can be read using SELECT statements. These data records are stored in Dataobjects such as variables, structure and Internal table.

The SELECT statement can be used in the following ways.

  • Reading a single line of record.
  • Reading more lines of record.
  • Reading more lines of record inside a loop.

Reading a single line of record:

              ‘SINGLE’ keyword is used in the select statement to read a single record from the Database table. The first line of record is selected in the Database table.

DATA ls_contact TYPE ztt_db_table2.

SELECT SINGLE * FROM ztt_db_table2 INTO ls_contact.

WRITE:/ ls_contact-contact_id.

Reading more lines of record:

              More than one lines of record can be read from the Database table using the addition ‘INTO’ or ‘APPENDING’. The Addition ‘INTO’ will overwrite the records in the internal table, if it already has some records. The Addition ‘APPENDING’ will not overwrite the records. It will hold the existing records in the internal table. It appends to these existing records. using the ‘SELCT DISTINCT *’, the Duplicate records can be ignored while writing into the Database table.

Using the Addition ‘UP TO’ … ‘ROWS’, the number of records selected can be constrained.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

DATA ls_contact TYPE ztt_db_table2.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

LOOP AT lt_contact INTO ls_contact.

              WRITE:/ ls_contact-contact_id.

ENDLOOP.

APPENDING:

SELECT * FROM ztt_db_table2 APPENDING TABLE lt_contact.

UPTO … ROWS…:

SELECT *FROM ztt_db_table2 INTO TABLE lt_contact UP TO 5 ROWS.

DISTINCT:

SELECT DISTINCT *FROM ztt_db_table2 INTO TABLE lt_contact.

Reading more lines of record inside a loop:

The lines of record can only be stored in a structure or variable. It cannot be stored inside an internal table. The ‘END SELECT’ keyword comes at the end. The line of record are processed between the ‘SELECT’ and ‘ENDSELECT’.

DATA ls_contact TYPE ztt_db_table2.

SELECT * FROM ztt_db_table2 INTO ls_contact.

              WRITE: / ls_contact-contact_id.

ENDSELECT.

The LOOP AT … ASSIGNING is preferred over the ‘SELECT… ENDSELECT’.

How to specify the column names in query.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

The below query can be used before ABAP version 7.40 SP05.

SELECT contact_id contact_name FROM ztt_db_table2 INTO TABLE ztt_db_table2.

After the release of ABAP version 7.40 SP05, the column names can be separated by comma. But the internal table must be specified with @.

SELECT contact_id, contact_name FROM ztt_db_table2 INTO TABLE @ztt_db_table2.

How to specify the target columns in ‘SELECT’ statement:

TYPE: BEGIN OF st_struct,

 contact_name TYPE ztt_db_table2-contact_name,

               contact_address TYPE ztt_db_table2-contact_address

END OF st_struct.

TYPES: tt_contact TYPE TABLE OF st_struct.

DATA pt_contact TYPE tt_contact.

SELECT * FROM ztt_db_table2 INTO CORRESPONDING FIELDS OF TABLE pt_contact.

When the components of the target internal table is different from the components of the database table, then the keyword ‘INTO CORRESPONDING FIELDS OF TABLE’ can be used. Through the `CORRESPONDING FIELDS OF´ keyword, the data records are written to the target internal table. The components of pt_contact are different from ztt_db_table2.

How to explicitly specify the Target columns in SELECT statement:

              The contents of the Database table can also be written to variables. This can be specifically used when the source and target column are different.

DATA lv_contact_id TYPE ztt_db_table2-contact_id.

DATA lv_contact_name TYPE ztt_db_table2-contact_name.

SELECT SINGLE contact_id contact_name FROM ztt_db_table2 INTO (lv_contact_id, lv_contact_name).