The only way of reading data from a database table is using select statements, in the below example we will read data from the MARA table in various ways.
||Means getting data into an internal table|
|Means getting data into |
|Means getting data in the common fields of database table and user defined internal table |
|Means getting data of the common fields of database tables and work area|
Read whole data from MARA
To read all records from MARA table, we use below code
data :it_mara type table of mara ." Declare internal table of type MARA Select * from MARA into table it_mara ." Read all records from MARA table and store in it_mara internal table
Read single record from MARA based on where condition
data :wa_mara type mara ." Declare work area of type MARA, because we are getting only one record Select single * from MARA into wa_mara where matnr= '00001' ." Read one recordsfrom MARA table and store in wa_mara work areaOR data :it_mara type table of mara ." Declare internal table of type MARA select * from MARA into table it_mara where matnr= '00001' ." Read all records from MARA table where MATNR is 00001, MATNR is a key field .
Reading data into the corresponding fields
INTO CORRESPONDING FIELDS of statement, we can get data into a user defined internal table.
As per performance standards, this method is not preferable
:BEGIN OF TY_MARA, MATNR TYPE MARA-MATNR, MTART TYPE MARA-MTART, MEINS TYPE MARA-MEINS, MBRSH TYPE MARA-MBRSH, END OF TY_MARA. DATA :IT_MARA TYPE TABLE OF TY_MARA ."Declare internal table of type user defined table. SELECT * FROM MARA INTO CORRESPONDING FIELD OF TABLE IT_MARA ." Here we are getting data from <code>MARA</code> table into internal table <code>IT_MARA </code> which contains only four fields.
Reading data into user defined internal table
Reading data from a database table into a user defined table.
This method is advisable as it gets limited fields from database table
TYPES : BEGIN OF TY_MARA, MATNR TYPE MARA-MATNR, MTART TYPE MARA-MTART, MEINS TYPE MARA-MEINS, MBRSH TYPE MARA-MBRSH, END OF TY_MARA. DATA
:IT_MARA TYPE TABLE OF TY_MARA. " Declare a Internal table of user defined type SELECT MATNR, MTART, MEINS, MBRSH FROM MARA INTO TABLE IT_MARA. " Get list of fields Data Into internal table " Now data of four fields is available in IT_MARA