Professional Documents
Culture Documents
Reading Data From Databases Tables Using OPEN SQL (SELECT) Statements in Sap Abap
Reading Data From Databases Tables Using OPEN SQL (SELECT) Statements in Sap Abap
SAP ABAP
The only way of reading data from a database table is using select statements, in the below example
we will read data from MARA table in various ways.
Operation Explanation
INTO TABLE ITAB Means getting data into an internal table ITAB from database table
INTO WA Means getting data into WA work area ( Work area can store only one
record )
INTO CORRESPONDING FIELDS Means getting data of common fields of database table and user defined
OF TABLE ITAB
internal table ITAB( Here ITAB is a user defined internal table )
INTO CORRESPONDING FIELDS Means getting data of common fields of database table and work area WA
OF WA
( Here WA is a work area )
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
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 records
from MARA table and store in wa_mara work area
OR
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 .
By using SELECT SINGLE and SELECT UPTO 1 ROWS we can able to read single record from a
database table .
Used to read exact record from database table. Used to read appropriate record from database table.
To read exact record from database table we We can read appropriate record from database table, we
need to provide all key fields. may not need to provide all key fields.
This statement should be used only if all the This statement should be used only if we have some key
key fields are available. fields or no key fields.
SELECT * FROM MARA INTO WA_MARA UP TO 50 ROWS WHERE MTART = 'FERT'. " Read 50
appropriate records from MARA table here MTART is not a keyfield
ENDSELECT.
LOOP AT IT_MARA INTO WA_MARA.
write :/ wa_mara-matnr, wa_mara-mtart, wa_mara-meins. " Print data to screen
ENDLOOP.
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.
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
Program : ZSELECT_STATEMENTS
REPORT ZSELECT_STATEMENTS.
*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
*LOOP.
*ENDLOOP.
**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 = '000000000000000003' .
" Read exact record from MARA table
*Read one record from MARA table and store in work area.
* ( OR )
SELECT * FROM MARA INTO WA_MARA UP TO 1 ROWS WHERE MTART = 'FERT'. " Read app
ropriate record from MARA table
** here MTART is not a keyfield
ENDSELECT.
write:/ wa_mara-matnr, wa_mara-mtart, wa_mara-meins. " Print data to screen
skip 2.
SELECT * FROM MARA INTO WA_MARA UP TO 50 ROWS WHERE MTART = 'FERT'. " Read 50
appropriate records from MARA table
** here MTART is not a keyfi
eld
write:/ wa_mara-matnr, wa_mara-mtart, wa_mara-meins.
ENDSELECT.
*data : it_mara type table of mara . " Declare internal table of type MARA
*select * from MARA into table it_mara where matnr = '000000000000000003' .
" Read all records from MARA table where MATNR is 00001, MATNR is a key fie
ld .
*LOOP.
*ENDLOOP.
**Reading data into corresponding fields
*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 internal table of type use
r defined table.
*
*SELECT * FROM MARA INTO CORRESPONDING FIELDS OF TABLE IT_MARA . " Here we
are getting data from MARA table
** into internal table IT_MARA which contains only
four fields.
* LOOP.
* ENDLOOP.
**Reading data into user defined internal 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 d
efined 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
* LOOP.
* ENDLOOP.
skip 4.
**SELECT DISTINCT eliminates duplicates records of a column of a table.
TYPES: BEGIN OF ty_mtart,
mtart TYPE mara-mtart,
END OF ty_mtart.
DATA: it_mtart TYPE TABLE OF ty_mtart,
wa_mtart TYPE ty_mtart.
SELECT DISTINCT mtart FROM mara INTO TABLE it_mtart UP TO 5 ROWS.
LOOP AT it_mtart INTO wa_mtart.
WRITE:/ wa_mtart-mtart.
ENDLOOP.
** Example for SELECT WITH BYPASSING BUFFER
* TYPES: BEGIN OF ty_mara,
* matnr TYPE mara-matnr,
* mtart TYPE mara-mtart,
* END OF ty_mara.
*DATA: it_mara TYPE TABLE OF ty_mara,
* wa_mara TYPE ty_mara.
*
*START-OF-SELECTION.
* SELECT matnr mtart FROM mara INTO TABLE it_mara BYPASSING BUFFER.
*
* LOOP AT it_mara INTO wa_mara.
* WRITE:/ wa_mara-matnr, wa_mara-mtart.
* ENDLOOP.
UPDATE
Is used to update data in database table, data can be inserted into database table using two ways.
Update record from internal table
data: it_mara type TABLE OF mara,
wa_mara type mara.
START-OF-SELECTION.
wa_mara-matnr = '000000000000000001'.
wa_mara-ernam = 'MMUSER'.
wa_mara-mtart = 'CRSA'.
wa_mara-pstat = 'INFOSYS'.
wa_mara-mbrsh = 'M'.
Append wa_mara to it_mara.
wa_mara-matnr = '000000000000000002'.
wa_mara-ernam = 'MMUSER'.
wa_mara-mtart = '1ROH'.
wa_mara-pstat = 'IBM'.
wa_mara-mbrsh = 'M'.
Append wa_mara to it_mara.
if sy-subrc eq 0.
UPDATE mara FROM TABLE it_mara.
message 'MARA Table Record updated' type 'I'.
endif.
parameter: p_matnr type mara-matnr.
data: wa_mara type mara.
************************************************
START-OF-SELECTION.
select single * from mara
into wa_mara
where matnr eq p_matnr.
wa_mara-aenam = 'MMUSER'.
wa_mara-vpsta = 'DCBA'.
wa_mara-pstat = 'ABCD'.
if sy-subrc eq 0.
UPDATE mara FROM wa_mara.
message 'MARA Table Record updated' type 'I'.
endif.
REPORT ZUPDATE_PRPS.
parameter: p_wbs type prps-pspnr,
p_value type prps-fakkz default 'X'.
data: wa_fakkz type prps-fakkz.
START-OF-SELECTION.
select single fakkz
into wa_fakkz
from prps
where pspnr eq p_wbs.
if sy-subrc eq 0.
update prps set fakkz = p_value where PSPNR eq p_wbs.
if p_value is initial.
message 'Billing element field has been unchecked' type 'I'.
else.
message 'Billing element field has been checked' type 'I'.
endif.
else.
message 'WBS element not found' type 'I'.
endif.
****************************
REPORT ZUPDATE_MARA.
parameter: p_matnr type mara-matnr,
p_pstat type PSTAT_D.
data: wa_pstat type mara-pstat.
START-OF-SELECTION.
select single pstat
into wa_pstat
from mara
where matnr eq p_matnr.
if sy-subrc eq 0.
update mara set pstat = p_pstat where matnr eq p_matnr.
if p_pstat is initial.
message 'Maintenance status field has been not modified' type 'I'.
else.
message 'Maintenance status field has been modified' type 'I'.
endif.
else.
message 'Material number not found' type 'I'.
endif.
INSERT
Is used to insert data into a database table, data can be inserted into database table using two ways.
Insert record from internal table
MODIFY
Is used to modify data in database table, data can be inserted into database table using two ways.
Modify record from internal table
*TABLES: mara.
*SELECT-OPTIONS: s_matnr for mara-matnr.
*
*data: IT_MARA TYPE TABLE OF MARA,
* wa_mara type mara.
*
*START-OF-SELECTION.
*
*wa_mara-matnr = '000000000000000005'.
*wa_mara-ersda = '20160721'.
*wa_mara-ernam = 'MMUSER'.
*wa_mara-laeda = '20160723'.
*wa_mara-aenam = 'MMUSER'.
*wa_mara-vpsta = 'KELBx'.
*wa_mara-pstat = 'KELB'.
*Append wa_mara to it_mara.
*
*wa_mara-matnr = '000000000000000002'.
*wa_mara-ersda = '20160721'.
*wa_mara-ernam = 'MMUSER'.
*wa_mara-laeda = '20160723'.
*wa_mara-aenam = 'MMUSER'.
*wa_mara-vpsta = 'KELBx'.
*wa_mara-pstat = 'KELB'.
*Append wa_mara to it_mara.
*
* if sy-subrc eq 0.
* MODIFY mara FROM TABLE it_mara.
*
*message 'MARA Table Record updated' type 'I'.
*endif.
DELETE
Is used to delete data from database table, data can be deleted into database table using two ways.
Delete record from internal table
*data: wa_mara type mara.
*
*START-OF-SELECTION.
*
*wa_mara-matnr = '000000000000001898'.
*
*if sy-subrc eq 0.
*
*DELETE mara from wa_mara.
*
*message 'MARA Table Record Deleted' type 'I'.
*endif.
**--------------------------------
*TABLES: mara.
*SELECT-OPTIONS: s_matnr for mara-matnr.
*
*data: IT_MARA TYPE TABLE OF MARA,
* wa_mara type mara.
*
*START-OF-SELECTION.
*
*wa_mara-matnr = '000000000000001872'.
*Append wa_mara to it_mara.
*
*wa_mara-matnr = '000000000000001873'.
*Append wa_mara to it_mara.
*
*wa_mara-matnr = '000000000000001874'.
*Append wa_mara to it_mara.
*
* if sy-subrc eq 0.
* DELETE mara FROM TABLE it_mara.
*
*message 'MARA Table Record Deleted' type 'I'.
*endif.
SELECT DISTINCT is a SQL Select query, which is used to get distinct values of a column from a
database table.
SELECT DISTINCT eliminates duplicates records of a column of a table.