ABAP SELECT Statement Within SAP
ABAP SELECT Statement Within SAP
ABAP SELECT Statement Within SAP
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).
*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
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.
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.
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
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.