Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Reading data from databases tables using OPEN SQL (SELECT) statements in

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 )

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

SELECT SINGLE SELECT UP TO 1 ROWS

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.

Example SELECT SINGLE

DATA : WA_MARA TYPE MARA. " Declare work area


SELECT SINGLE * FROM MARA INTO WA_MARA WHERE MATNR = '0001'. " Read exact record from
MARA table
write : wa_mara-matnr, wa_mara-mtart, wa_mara-meins. " Print data to screen

Example SELECT UPTO

DATA : WA_MARA TYPE MARA. " Declare work area


SELECT * FROM MARA INTO WA_MARA UP TO 1 ROWS WHERE MTART = 'FERT'. " Read appropriate
record from MARA table here MTART is not a keyfield
ENDSELECT.

We can read multiple records using SELECT UPTO


DATA : IT_MARA TYPE TABLE OF MARA. " Declare internal table
DATA : WA_MARA TYPE MARA. " Declare work area

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.

Reading data into corresponding fields


By using INTO CORRESPONDING FIELDS of statement we can get data into a user defined internal table.
As per performance standards, this method is not preferable

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

DATABASE TABLES OPERATIONS

UPDATE
Is used to update data in database table, data can be inserted into database table using two ways.
Update record from internal table

Syntax: UPDATE FROM TABLE ITAB. Program: ZUPDATE_ITAB


REPORT  ZUPDATE_ITAB.
*TABLES: mara.
*SELECT-OPTIONS: s_matnr for mara-matnr.
*PARAMETERS: p_matnr TYPE mara-matnr.

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.

Update record from work area

Syntax: UPDATE FROM . Program: ZUPDATE_WA


REPORT  ZUPDATE_WA.

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.

Update single field from work area


Program: ZUPDATE_PRPS , ZUPDATE_MARA
update prps set fakkz = p_value where PSPNR eq p_wbs.

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

Syntax: INSERT DBTAB FROM TABLE ITAB .

Insert record from work area

Syntax: INSERT DBTAB FROM WA .

MODIFY
Is used to modify data in database table, data can be inserted into database table using two ways.
Modify record from internal table

Syntax: MODIFY DBTAB FROM TABLE ITAB. Program: ZMODIFY

Modify record from work area

Syntax: MODIFY DBTAB FROM WA. Program: ZMODIFY


REPORT  ZMODIFY.
*TABLES SCUSTOM.
*SCUSTOM-ID        = '00000009'.
*SCUSTOM-NAME      = 'Robinson'.
*SCUSTOM-POSTCODE  = '69542'.
*SCUSTOM-CITY      = 'Heidelberg'.
*SCUSTOM-CUSTTYPE  = 'P'.
*SCUSTOM-DISCOUNT  = '003'.
*SCUSTOM-TELEPHONE = '06201/44889'.
*
*MODIFY SCUSTOM.
**************************************************
*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 = 'ABAPUSER'.
*  wa_mara-vpsta = 'AAAA'.
*  wa_mara-pstat = 'BBBB'.
*if sy-subrc eq 0.
*   MODIFY mara FROM wa_mara.
*
*message 'MARA Table Record updated' 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 = '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

Syntax: DELETE DBTAB FROM TABLE ITAB. Program: ZDELETE_DB

Delete record from work area

Syntax: DELETE DBTAB FROM WA. Program: ZDELETE_DB


REPORT  ZDELETE_DB.

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

UPDATE: updates the existing record


MODIFY: updates the existing record, if record not found it creates new record

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.

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.
START-OF-SELECTION.
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.

SELECT with BYPASSING BUFFER


Whenever we use open SQL statements to get fetch data in SAP, it will get data from buffer
area(depends on table buffer settings) for better performance, but in real world scenarios some
tables may updated very frequently(milliseconds), we may need to bypass buffer to get real-time
data, in that case we will bypass buffer using 'BYPASSING BUFFER' keyword.

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.

You might also like