ABAP Select data from SAP table EKKO into internal table

Get Example source ABAP code based on a different SAP table
  

Below is a number of ABAP code snippets to demonstrate how to select data from SAP EKKO table and store it within an internal table, including using the newer @DATA inline declaration methods. It also shows you various ways to process this data using ABAP work area, inline declaration or field symbols including executing all the relevant CONVERSION_EXIT routines specific to EKKO. See here for more generic Select statement tips.

Sometimes data within SAP is stored within the database table in a different format to what it is displayed to the user. These input/output conversation FM routines are what translates the data between the two formats.

There is also a full declaration of the EKKO table where each field has a char/string type for you to simply copy and paste. This allows you to use processing that is only available to these field types such as the CONCATENATE statement.

DATA: IT_EKKO TYPE STANDARD TABLE OF EKKO,
      WA_EKKO TYPE EKKO,
      GD_STR TYPE STRING.

DATA: lo_typedescr type REF TO cl_abap_typedescr.
DATA: lv_fieldname type fieldname.

FIELD-SYMBOLS: <FIELD> TYPE any.
FIELD-SYMBOLS: <EKKO> TYPE EKKO.

*Process all fields in table header/work area as string values
  PERFORM process_as_string_field_values CHANGING wa_EKKO.

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM EKKO
  INTO TABLE IT_EKKO.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM EKKO
*  INTO TABLE @DATA(IT_EKKO2).
*--Further methods of using ABAP code to  select data from SAP database tables

*You can also declare the header/work area using the in-line DATA declaration method
READ TABLE IT_EKKO INDEX 1 INTO DATA(WA_EKKO2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_EKKO ASSIGNING <EKKO>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<EKKO>-MANDT = 1.
<EKKO>-EBELN = 1.
<EKKO>-BUKRS = 1.
<EKKO>-BSTYP = 1.
<EKKO>-BSART = 1.
ENDLOOP.

LOOP AT IT_EKKO INTO WA_EKKO.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_EKKO-BSAKZ, sy-vline,
WA_EKKO-LOEKZ, sy-vline,
WA_EKKO-STATU, sy-vline,
WA_EKKO-AEDAT, sy-vline,
WA_EKKO-ERNAM, sy-vline,
WA_EKKO-PINCR, sy-vline.
ENDLOOP. *Add any further fields from structure WA_EKKO you want to display... WRITE:/ sy-uline. * Aternatively use generic code to Write field values (and NAME) to screen report DO. ASSIGN COMPONENT sy-index OF STRUCTURE wa_EKKO TO <field>. IF sy-subrc <> 0. EXIT. ENDIF. WRITE:/ 'Field Value', <field>, sy-vline. gd_str = <field> . lo_typedescr ?= CL_ABAP_DATADESCR=>DESCRIBE_BY_DATA( <field> ). lv_fieldname = lo_typedescr->GET_RELATIVE_NAME( ). WRITE:/ 'Field Name', lv_fieldname. ENDDO. *Redo loop but convert all fields from internal to out value LOOP AT IT_EKKO INTO WA_EKKO. *Write horizonal line to screen report. WRITE:/ sy-uline. *Convert all fields to display/output versions using conversion routines PERFORM convert_all_field_values CHANGING wa_EKKO. ENDLOOP. *&---------------------------------------------------------------------* *& Form convert_all_field_values *&---------------------------------------------------------------------* FORM convert_all_field_values CHANGING p_EKKO LIKE wa_EKKO. DATA: ld_input(1000) TYPE c, ld_output(1000) TYPE C.

*Conversion exit ALPHA, internal->external for field EBELN CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-EBELN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-EBELN.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field LIFNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-LIFNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-LIFNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ISOLA, internal->external for field SPRAS CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_EKKO-SPRAS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-SPRAS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit EXCRT, internal->external for field WKURS CALL FUNCTION 'CONVERSION_EXIT_EXCRT_OUTPUT' EXPORTING input = WA_EKKO-WKURS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-WKURS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field AUSNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-AUSNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-AUSNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field LLIEF CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-LLIEF IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-LLIEF.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field KUNNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-KUNNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-KUNNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field KONNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-KONNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-KONNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field LBLIF CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-LBLIF IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-LBLIF.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field STAFO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-STAFO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-STAFO.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field LIFRE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-LIFRE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-LIFRE.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field LOGSY CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-LOGSY IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-LOGSY.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ADRNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-ADRNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-ADRNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ADDNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-ADDNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-ADDNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field REVNO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-REVNO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-REVNO.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field MSR_ID CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-MSR_ID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-MSR_ID.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field HANDOVERLOC CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-HANDOVERLOC IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-HANDOVERLOC.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field SOURCE_LOGSYS CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_EKKO-SOURCE_LOGSYS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_EKKO-SOURCE_LOGSYS.
WRITE:/ 'New Value:', ld_input.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_EKKO_STR,
MANDT TYPE STRING,
EBELN TYPE STRING,
BUKRS TYPE STRING,
BSTYP TYPE STRING,
BSART TYPE STRING,
BSAKZ TYPE STRING,
LOEKZ TYPE STRING,
STATU TYPE STRING,
AEDAT TYPE STRING,
ERNAM TYPE STRING,
PINCR TYPE STRING,
LPONR TYPE STRING,
LIFNR TYPE STRING,
SPRAS TYPE STRING,
EKORG TYPE STRING,
EKGRP TYPE STRING,
WAERS TYPE STRING,
WKURS TYPE STRING,
KUFIX TYPE STRING,
BEDAT TYPE STRING,
KDATB TYPE STRING,
KDATE TYPE STRING,
BWBDT TYPE STRING,
ANGDT TYPE STRING,
BNDDT TYPE STRING,
GWLDT TYPE STRING,
AUSNR TYPE STRING,
ANGNR TYPE STRING,
IHRAN TYPE STRING,
IHREZ TYPE STRING,
VERKF TYPE STRING,
TELF1 TYPE STRING,
LLIEF TYPE STRING,
KUNNR TYPE STRING,
KONNR TYPE STRING,
ABGRU TYPE STRING,
AUTLF TYPE STRING,
WEAKT TYPE STRING,
RESWK TYPE STRING,
LBLIF TYPE STRING,
INCO1 TYPE STRING,
INCO2 TYPE STRING,
KTWRT TYPE STRING,
SUBMI TYPE STRING,
KNUMV TYPE STRING,
KALSM TYPE STRING,
STAFO TYPE STRING,
LIFRE TYPE STRING,
EXNUM TYPE STRING,
UNSEZ TYPE STRING,
LOGSY TYPE STRING,
UPINC TYPE STRING,
STAKO TYPE STRING,
FRGGR TYPE STRING,
FRGSX TYPE STRING,
FRGKE TYPE STRING,
FRGZU TYPE STRING,
FRGRL TYPE STRING,
LANDS TYPE STRING,
LPHIS TYPE STRING,
ADRNR TYPE STRING,
STCEG_L TYPE STRING,
STCEG TYPE STRING,
ABSGR TYPE STRING,
ADDNR TYPE STRING,
KORNR TYPE STRING,
MEMORY TYPE STRING,
PROCSTAT TYPE STRING,
RLWRT TYPE STRING,
REVNO TYPE STRING,
SCMPROC TYPE STRING,
REASON_CODE TYPE STRING,
MEMORYTYPE TYPE STRING,
RETTP TYPE STRING,
RETPC TYPE STRING,
DPTYP TYPE STRING,
DPPCT TYPE STRING,
DPAMT TYPE STRING,
DPDAT TYPE STRING,
MSR_ID TYPE STRING,
HIERARCHY_EXISTS TYPE STRING,
THRESHOLD_EXISTS TYPE STRING,
LEGAL_CONTRACT TYPE STRING,
DESCRIPTION TYPE STRING,
RELEASE_DATE TYPE STRING,
VSART TYPE STRING,
HANDOVERLOC TYPE STRING,
FORCE_ID TYPE STRING,
FORCE_CNT TYPE STRING,
RELOC_ID TYPE STRING,
RELOC_SEQ_ID TYPE STRING,
SOURCE_LOGSYS TYPE STRING,
PLC TYPE STRING,
SMB TYPE STRING,
ECC TYPE STRING,
IPP TYPE STRING,
CUR TYPE STRING,
ECO TYPE STRING,
PCO TYPE STRING,
DPT TYPE STRING,
TOP TYPE STRING,
SUS TYPE STRING,
PCN TYPE STRING,
TRU TYPE STRING,
PCY TYPE STRING,
PCD TYPE STRING,
PBN TYPE STRING,
AMN TYPE STRING,
DNO TYPE STRING,
POSTAT TYPE STRING,
VZSKZ TYPE STRING,
POHF_TYPE TYPE STRING,
EQ_EINDT TYPE STRING,
EQ_WERKS TYPE STRING,
FIXPO TYPE STRING,
EKGRP_ALLOW TYPE STRING,
WERKS_ALLOW TYPE STRING,
CONTRACT_ALLOW TYPE STRING,
PSTYP_ALLOW TYPE STRING,
FIXPO_ALLOW TYPE STRING,
KEY_ID_ALLOW TYPE STRING,
AUREL_ALLOW TYPE STRING,
DELPER_ALLOW TYPE STRING,
EINDT_ALLOW TYPE STRING,
LTSNR_ALLOW TYPE STRING,
OTB_LEVEL TYPE STRING,
OTB_COND_TYPE TYPE STRING,
KEY_ID TYPE STRING,
OTB_VALUE TYPE STRING,
OTB_CURR TYPE STRING,
OTB_RES_VALUE TYPE STRING,
OTB_SPEC_VALUE TYPE STRING,
SPR_RSN_PROFILE TYPE STRING,
BUDG_TYPE TYPE STRING,
OTB_STATUS TYPE STRING,
OTB_REASON TYPE STRING,
CHECK_TYPE TYPE STRING,
CON_OTB_REQ TYPE STRING,
CON_PREBOOK_LEV TYPE STRING,
CON_DISTR_LEV TYPE STRING,END OF T_EKKO_STR. DATA: WA_EKKO_STR type T_EKKO_STR. DATA: ld_text TYPE string. LOOP AT IT_EKKO INTO WA_EKKO. MOVE-CORRESPONDING wa_EKKO TO WA_EKKO_STR. CONCATENATE: sy-vline
WA_EKKO_STR-MANDT sy-vline
WA_EKKO_STR-EBELN sy-vline
WA_EKKO_STR-BUKRS sy-vline
WA_EKKO_STR-BSTYP sy-vline
WA_EKKO_STR-BSART sy-vline
WA_EKKO_STR-BSAKZ sy-vline
WA_EKKO_STR-LOEKZ sy-vline
WA_EKKO_STR-STATU sy-vline
WA_EKKO_STR-AEDAT sy-vline
WA_EKKO_STR-ERNAM sy-vline
WA_EKKO_STR-PINCR sy-vline
WA_EKKO_STR-LPONR sy-vline
WA_EKKO_STR-LIFNR sy-vline
WA_EKKO_STR-SPRAS sy-vline
WA_EKKO_STR-EKORG sy-vline
WA_EKKO_STR-EKGRP sy-vline
WA_EKKO_STR-WAERS sy-vline
WA_EKKO_STR-WKURS sy-vline
WA_EKKO_STR-KUFIX sy-vline
WA_EKKO_STR-BEDAT sy-vline
WA_EKKO_STR-KDATB sy-vline
WA_EKKO_STR-KDATE sy-vline
WA_EKKO_STR-BWBDT sy-vline
WA_EKKO_STR-ANGDT sy-vline
WA_EKKO_STR-BNDDT sy-vline
WA_EKKO_STR-GWLDT sy-vline
WA_EKKO_STR-AUSNR sy-vline
WA_EKKO_STR-ANGNR sy-vline
WA_EKKO_STR-IHRAN sy-vline
WA_EKKO_STR-IHREZ sy-vline
WA_EKKO_STR-VERKF sy-vline
WA_EKKO_STR-TELF1 sy-vline
WA_EKKO_STR-LLIEF sy-vline
WA_EKKO_STR-KUNNR sy-vline
WA_EKKO_STR-KONNR sy-vline
WA_EKKO_STR-ABGRU sy-vline
WA_EKKO_STR-AUTLF sy-vline
WA_EKKO_STR-WEAKT sy-vline
WA_EKKO_STR-RESWK sy-vline
WA_EKKO_STR-LBLIF sy-vline
WA_EKKO_STR-INCO1 sy-vline
WA_EKKO_STR-INCO2 sy-vline
WA_EKKO_STR-KTWRT sy-vline
WA_EKKO_STR-SUBMI sy-vline
WA_EKKO_STR-KNUMV sy-vline
WA_EKKO_STR-KALSM sy-vline
WA_EKKO_STR-STAFO sy-vline
WA_EKKO_STR-LIFRE sy-vline
WA_EKKO_STR-EXNUM sy-vline
WA_EKKO_STR-UNSEZ sy-vline
WA_EKKO_STR-LOGSY sy-vline
WA_EKKO_STR-UPINC sy-vline
WA_EKKO_STR-STAKO sy-vline
WA_EKKO_STR-FRGGR sy-vline
WA_EKKO_STR-FRGSX sy-vline
WA_EKKO_STR-FRGKE sy-vline
WA_EKKO_STR-FRGZU sy-vline
WA_EKKO_STR-FRGRL sy-vline
WA_EKKO_STR-LANDS sy-vline
WA_EKKO_STR-LPHIS sy-vline
WA_EKKO_STR-ADRNR sy-vline
WA_EKKO_STR-STCEG_L sy-vline
WA_EKKO_STR-STCEG sy-vline
WA_EKKO_STR-ABSGR sy-vline
WA_EKKO_STR-ADDNR sy-vline
WA_EKKO_STR-KORNR sy-vline
WA_EKKO_STR-MEMORY sy-vline
WA_EKKO_STR-PROCSTAT sy-vline
WA_EKKO_STR-RLWRT sy-vline
WA_EKKO_STR-REVNO sy-vline
WA_EKKO_STR-SCMPROC sy-vline
WA_EKKO_STR-REASON_CODE sy-vline
WA_EKKO_STR-MEMORYTYPE sy-vline
WA_EKKO_STR-RETTP sy-vline
WA_EKKO_STR-RETPC sy-vline
WA_EKKO_STR-DPTYP sy-vline
WA_EKKO_STR-DPPCT sy-vline
WA_EKKO_STR-DPAMT sy-vline
WA_EKKO_STR-DPDAT sy-vline
WA_EKKO_STR-MSR_ID sy-vline
WA_EKKO_STR-HIERARCHY_EXISTS sy-vline
WA_EKKO_STR-THRESHOLD_EXISTS sy-vline
WA_EKKO_STR-LEGAL_CONTRACT sy-vline
WA_EKKO_STR-DESCRIPTION sy-vline
WA_EKKO_STR-RELEASE_DATE sy-vline
WA_EKKO_STR-VSART sy-vline
WA_EKKO_STR-HANDOVERLOC sy-vline
WA_EKKO_STR-FORCE_ID sy-vline
WA_EKKO_STR-FORCE_CNT sy-vline
WA_EKKO_STR-RELOC_ID sy-vline
WA_EKKO_STR-RELOC_SEQ_ID sy-vline
WA_EKKO_STR-SOURCE_LOGSYS sy-vline
WA_EKKO_STR-PLC sy-vline
WA_EKKO_STR-SMB sy-vline
WA_EKKO_STR-ECC sy-vline
WA_EKKO_STR-IPP sy-vline
WA_EKKO_STR-CUR sy-vline
WA_EKKO_STR-ECO sy-vline
WA_EKKO_STR-PCO sy-vline
WA_EKKO_STR-DPT sy-vline
WA_EKKO_STR-TOP sy-vline
WA_EKKO_STR-SUS sy-vline
WA_EKKO_STR-PCN sy-vline
WA_EKKO_STR-TRU sy-vline
WA_EKKO_STR-PCY sy-vline
WA_EKKO_STR-PCD sy-vline
WA_EKKO_STR-PBN sy-vline
WA_EKKO_STR-AMN sy-vline
WA_EKKO_STR-DNO sy-vline
WA_EKKO_STR-POSTAT sy-vline
WA_EKKO_STR-VZSKZ sy-vline
WA_EKKO_STR-POHF_TYPE sy-vline
WA_EKKO_STR-EQ_EINDT sy-vline
WA_EKKO_STR-EQ_WERKS sy-vline
WA_EKKO_STR-FIXPO sy-vline
WA_EKKO_STR-EKGRP_ALLOW sy-vline
WA_EKKO_STR-WERKS_ALLOW sy-vline
WA_EKKO_STR-CONTRACT_ALLOW sy-vline
WA_EKKO_STR-PSTYP_ALLOW sy-vline
WA_EKKO_STR-FIXPO_ALLOW sy-vline
WA_EKKO_STR-KEY_ID_ALLOW sy-vline
WA_EKKO_STR-AUREL_ALLOW sy-vline
WA_EKKO_STR-DELPER_ALLOW sy-vline
WA_EKKO_STR-EINDT_ALLOW sy-vline
WA_EKKO_STR-LTSNR_ALLOW sy-vline
WA_EKKO_STR-OTB_LEVEL sy-vline
WA_EKKO_STR-OTB_COND_TYPE sy-vline
WA_EKKO_STR-KEY_ID sy-vline
WA_EKKO_STR-OTB_VALUE sy-vline
WA_EKKO_STR-OTB_CURR sy-vline
WA_EKKO_STR-OTB_RES_VALUE sy-vline
WA_EKKO_STR-OTB_SPEC_VALUE sy-vline
WA_EKKO_STR-SPR_RSN_PROFILE sy-vline
WA_EKKO_STR-BUDG_TYPE sy-vline
WA_EKKO_STR-OTB_STATUS sy-vline
WA_EKKO_STR-OTB_REASON sy-vline
WA_EKKO_STR-CHECK_TYPE sy-vline
WA_EKKO_STR-CON_OTB_REQ sy-vline
WA_EKKO_STR-CON_PREBOOK_LEV sy-vline
WA_EKKO_STR-CON_DISTR_LEV sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.