You are on page 1of 4

ABAP SELECT statement within SAP - Example ABAP code to

demonstrate the SELECT command

The ABAP SELECT statement is the most fundamental function of writing ABAP
programs within SAP, allowing the retrieval of data from SAP database tables.
Below are a few examples of the various ways of selecting data ready for
processing.

Also check out the new @DATA inline syntax for selecting data into an internal
table, without the need for the separate data declaration

i.e. select ebeln, ebelp, statu, aedat, matnr, menge, meins, netpr, peinh
up to 10 rows
from ekpo
into TABLE @DATA(it_ekko_inline).

Select INTO internal table

*ABAP Code to demonstrate select into internal table command


TYPES: BEGIN OF t_bkpf,
* include structure bkpf.
bukrs LIKE bkpf-bukrs,
belnr LIKE bkpf-belnr,
gjahr LIKE bkpf-gjahr,
bldat LIKE bkpf-bldat,
monat LIKE bkpf-monat,
budat LIKE bkpf-budat,
xblnr LIKE bkpf-xblnr,
awtyp LIKE bkpf-awtyp,
awkey LIKE bkpf-awkey,
END OF t_bkpf.
DATA: it_bkpf TYPE STANDARD TABLE OF t_bkpf INITIAL SIZE 0,
wa_bkpf TYPE t_bkpf.

TYPES: BEGIN OF t_bseg,


*include structure bseg.
bukrs LIKE bseg-bukrs,
belnr LIKE bseg-belnr,
gjahr LIKE bseg-gjahr,
buzei LIKE bseg-buzei,
mwskz LIKE bseg-mwskz, "Tax code
umsks LIKE bseg-umsks, "Special G/L transaction type
prctr LIKE bseg-prctr, "Profit Centre
hkont LIKE bseg-hkont, "G/L account
xauto LIKE bseg-xauto,
koart LIKE bseg-koart,
dmbtr LIKE bseg-dmbtr,
mwart LIKE bseg-mwart,
hwbas LIKE bseg-hwbas,
aufnr LIKE bseg-aufnr,
projk LIKE bseg-projk,
shkzg LIKE bseg-shkzg,
kokrs LIKE bseg-kokrs,
END OF t_bseg.
DATA: it_bseg TYPE STANDARD TABLE OF t_bseg INITIAL SIZE 0,
wa_bseg TYPE t_bseg.

DATA: it_ekko TYPE STANDARD TABLE OF ekko.

*Select all fields of a SAP database table into in itab


SELECT *
FROM ekko
INTO TABLE it_ekko.

*Inline Version - (difference is you don't need the data declaration above)
select *
from ekpo
into TABLE @DATA(it_ekko2).
"see here for more info and examples of the inline @DATA & DATA syntax

*Select directly into an internal table


SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO TABLE it_bseg.

* Select directly into an internal table where fields are in a


* different order or not all fields are specified
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO CORRESPONDING FIELDS OF TABLE it_bseg.

Select... endselect statement

The SELECT..ENDSELECT statement acts like a loop command and any coding within it
will be performed on each loop pass. This syntax should generally be avoided unless it
only retrieves a small number of records and is only performed once i.e. it is not within
a nested loop.
*Select... endselect command
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO wa_bseg.

APPEND wa_bseg TO it_bseg.


ENDSELECT.

Select FOR ALL ENTRIES statement


The FOR ALL ENTRIES statement addition restricts the result to only those that exist
within the passed internal table. However if the for all entries table is initial(empty),
then all records will be retrieved.
*Select FOR ALL ENTRIES command
SELECT bukrs belnr gjahr bldat monat budat xblnr awtyp awkey
UP TO 100 ROWS
FROM bkpf
INTO TABLE it_bkpf.

IF sy-subrc EQ 0.
* The FOR ALL ENTRIES command only retrieves data which matches
* entries within a particular internal table.
SELECT bukrs belnr gjahr buzei mwskz umsks prctr hkont xauto koart
dmbtr mwart hwbas aufnr projk shkzg kokrs
FROM bseg
INTO TABLE it_bseg
FOR ALL ENTRIES IN it_bkpf
WHERE bukrs EQ it_bkpf-bukrs AND
belnr EQ it_bkpf-belnr AND
gjahr EQ it_bkpf-gjahr.
ENDIF.

Select SINGLE statement

The SELECT SINGLE addition only selects one record. Originally this required you to
include the full key within the where clause, and to retrieve 1 row without the full key
you had to use the "UP TO" addition. This is no longer the case and you can use SELECT
SINGLE without the full table key.
*Select Single with full table key retrieves ONLY one row if available
SELECT * "all fields
FROM bseg
INTO TABLE it_bseg
WHERE bukrs EQ wa_bseg-bukrs AND
belnr EQ wa_bseg-belnr AND
gjahr EQ wa_bseg-gjahr AND
buzei EQ wa_bseg-BUZEI

*Select Single without full table key. Syntactically correct and still
retrieves ONLY one row if available
SELECT * "all fields
FROM bseg
INTO TABLE it_bseg
WHERE bukrs EQ wa_bseg-bukrs AND
belnr EQ wa_bseg-belnr.

Select UP TO .. Rows

The select UP TO n ROWS addition selects up to a specific number of rows. "UP TO 1


ROWS" will select 1 row if 1 exists and "UP TO 1000 ROWS" will select up to 1000 rows
if they exist based on the selection criteria.

Please note that even if you have the UP TO 1000 ROWS addition it will still only select
based on the where clause, so it may still only select 10, 200, 300, 500 etc if that's all
it returns but not more than 1000.
*Select UP TO 1 ROWS - Retrieves ONLY one row if available
SELECT * "select all fields
FROM bseg
UP TO 1 ROWS
INTO TABLE it_bseg
WHERE bukrs EQ wa_bseg-bukrs AND
belnr EQ wa_bseg-belnr.

*Select UP TO 1000 ROWS - Retrieves no more than 1000 rows


SELECT * "select all fields
FROM bseg
UP TO 1000 ROWS
INTO TABLE it_bseg
WHERE bukrs EQ wa_bseg-bukrs AND
belnr EQ wa_bseg-belnr.