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.

Related Articles

Beginners Guide to learning SAP development starting with logging into SAP
ABAP Programming EVENTS in SAP
ABAP Function Module basics in SAP
DATA and @DATA Inline ABAP declarations available from release 7.40 to help make your code cleaner and more readable
ABAP Workbench Programming Techniques - BC402
ABAP rules to consider before creating a bespoke abap report or program
ABAP Subroutine basics in SAP
Get access to an SAP system for individual needs
SAP Base 64 encoding and decoding using ABAP code
Call web URl from ABAP report or SAP help documentation
Direct download, downloading sap objects
SAP Icons
SAP icons and some ABAP code to display them
SAP icons list and their associated codes
Internal Program Environment diplays all internal/external objects and operations used by an SAP program
minisap installation on your local pc to allow ABAP development, your own local SE80
SAP minisap installation on your local pc to allow ABAP development for free
SAP module based information such FI, HR, MM, PM, BW etc
Need an SAP ABAP program created?
SAP repository objects - List of useful standard and bespoke SAP repository objects
Retrieve SAP objects using transport entry in SE10 to restore objects that have been deleted
SAP Help for all areas for SAP ABAP Development inc ABAP, ALV, Web dynpro, bsp, HR, BW
ABAP tutorial - Logging into an SAP system for the first time
Manage and delete SAP sessions using ABAP code
SAP module areas
Increase & Decrease size of SAP editor text
ABAP development information and code examples for producing be-spoke SAP functionality
ABAP Development Info - Example code and information on various areas of ABAP development
SAP command field entries - box in top left corner
Force new page when printing abap source code
ABAP FIELD SYMBOL - Techniques for manupulating data using the FIELD-SYMBOL statement
Hiding ABAP Source Code so that it can not be viewed by anyone
ABAP Internal table declaration - Various methods of creating internal data structures and tables
Parameter ID - ABAP code to demonstrate how to set and get a parameter ID
RANGE statement - Example ABAP code to demonstrate the RANGE command
Change SAP logo in top right hand corner of SAP client
Create desktop Shortcut to SAP function
SAP Note assistant - Using transaction SNOTE to read and apply OSS note fix
VARYING command - Example ABAP code to demonstrate the VARYING command
Creating your first helloworld ABAP report in SAP