ABAP Select data from SAP table KGAL_ALL 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 KGAL_ALL 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 KGAL_ALL. 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 KGAL_ALL 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_KGAL_ALL TYPE STANDARD TABLE OF KGAL_ALL,
      WA_KGAL_ALL TYPE KGAL_ALL,
      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: <KGAL_ALL> TYPE KGAL_ALL.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM KGAL_ALL
*  INTO TABLE @DATA(IT_KGAL_ALL2).
*--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_KGAL_ALL INDEX 1 INTO DATA(WA_KGAL_ALL2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_KGAL_ALL ASSIGNING <KGAL_ALL>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<KGAL_ALL>-MANDT = 1.
<KGAL_ALL>-TAB = 1.
<KGAL_ALL>-CYCLE = 1.
<KGAL_ALL>-SDATE = 1.
<KGAL_ALL>-EDATE = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_KGAL_ALL-CRDATE, sy-vline,
WA_KGAL_ALL-PUSER, sy-vline,
WA_KGAL_ALL-MODDATE, sy-vline,
WA_KGAL_ALL-MODUSER, sy-vline,
WA_KGAL_ALL-LASTEXEC, sy-vline,
WA_KGAL_ALL-LEXECBY, sy-vline.
ENDLOOP. *Add any further fields from structure WA_KGAL_ALL 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_KGAL_ALL 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_KGAL_ALL INTO WA_KGAL_ALL. *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 ASACC CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KGAL_ALL-ASACC IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KGAL_ALL-ASACC.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field ERSCH CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KGAL_ALL-ERSCH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KGAL_ALL-ERSCH.
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_KGAL_ALL_STR,
MANDT TYPE STRING,
TAB TYPE STRING,
CYCLE TYPE STRING,
SDATE TYPE STRING,
EDATE TYPE STRING,
CRDATE TYPE STRING,
PUSER TYPE STRING,
MODDATE TYPE STRING,
MODUSER TYPE STRING,
LASTEXEC TYPE STRING,
LEXECBY TYPE STRING,
TIMEEXEC TYPE STRING,
ITERFLAG TYPE STRING,
ALART TYPE STRING,
IPKNZ TYPE STRING,
KUMUFLAG TYPE STRING,
XXKUMUFLAG TYPE STRING,
RCKUMUFLAG TYPE STRING,
PROC_GROUP TYPE STRING,
RKE_SPLIT TYPE STRING,
FM_DERIVE TYPE STRING,
GL_XBILK TYPE STRING,
FM_DERIVE_FONDS TYPE STRING,
FM_DERIVE_FKBER TYPE STRING,
FM_DERIVE_GRANT TYPE STRING,
FM_DERIVE_BUDPD TYPE STRING,
GL_LDGRP TYPE STRING,
PERIOD_LAG TYPE STRING,
PERIOD_DELTA TYPE STRING,
KALC_PRODPER TYPE STRING,
VARIABEL TYPE STRING,
SEQNR TYPE STRING,
NAME TYPE STRING,
SEQPOS TYPE STRING,
SRULE TYPE STRING,
RRULE TYPE STRING,
ACTIVE TYPE STRING,
NEGTEST TYPE STRING,
SPERCENT TYPE STRING,
SFFELD1 TYPE STRING,
SFFELD2 TYPE STRING,
SFFELD3 TYPE STRING,
SFFELD4 TYPE STRING,
SFFELD5 TYPE STRING,
SFFELD6 TYPE STRING,
SFFELD7 TYPE STRING,
SFFELD8 TYPE STRING,
SFFELD9 TYPE STRING,
SFFELD10 TYPE STRING,
FACURR TYPE STRING,
RFFELD1 TYPE STRING,
RFFELD2 TYPE STRING,
RFFELD3 TYPE STRING,
RFFELD4 TYPE STRING,
RFFELD5 TYPE STRING,
RFFELD6 TYPE STRING,
RFFELD7 TYPE STRING,
RFFELD8 TYPE STRING,
RFFELD9 TYPE STRING,
RFFELD10 TYPE STRING,
SCIP TYPE STRING,
RCDATA TYPE STRING,
ASACC TYPE STRING,
PAFLDF TYPE STRING,
PAFLDG TYPE STRING,
RRFELD1 TYPE STRING,
SORTFIELD TYPE STRING,
SENDER_NOT TYPE STRING,
MRULE TYPE STRING,
ABSCH TYPE STRING,
ERSCH TYPE STRING,
SWEIGHT_FACT TYPE STRING,
RWEIGHT_FACT TYPE STRING,
TXT TYPE STRING,
SETKIND TYPE STRING,
POS TYPE STRING,
FIELD TYPE STRING,
SETNAME TYPE STRING,
VALMIN TYPE STRING,
VALMAX TYPE STRING,
SETID TYPE STRING,
SRFLAG TYPE STRING,
FPOS TYPE STRING,
ELEMENT1 TYPE STRING,
ELEMENT2 TYPE STRING,
ELEMENT3 TYPE STRING,
ELEMENT4 TYPE STRING,
ELEMENT5 TYPE STRING,
ELEMENT6 TYPE STRING,
ELEMENT7 TYPE STRING,
ELEMENT8 TYPE STRING,
ELEMENT9 TYPE STRING,
ELEMENT10 TYPE STRING,
VALUE TYPE STRING,END OF T_EKKO_STR. DATA: WA_KGAL_ALL_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_KGAL_ALL_STR-MANDT sy-vline
WA_KGAL_ALL_STR-TAB sy-vline
WA_KGAL_ALL_STR-CYCLE sy-vline
WA_KGAL_ALL_STR-SDATE sy-vline
WA_KGAL_ALL_STR-EDATE sy-vline
WA_KGAL_ALL_STR-CRDATE sy-vline
WA_KGAL_ALL_STR-PUSER sy-vline
WA_KGAL_ALL_STR-MODDATE sy-vline
WA_KGAL_ALL_STR-MODUSER sy-vline
WA_KGAL_ALL_STR-LASTEXEC sy-vline
WA_KGAL_ALL_STR-LEXECBY sy-vline
WA_KGAL_ALL_STR-TIMEEXEC sy-vline
WA_KGAL_ALL_STR-ITERFLAG sy-vline
WA_KGAL_ALL_STR-ALART sy-vline
WA_KGAL_ALL_STR-IPKNZ sy-vline
WA_KGAL_ALL_STR-KUMUFLAG sy-vline
WA_KGAL_ALL_STR-XXKUMUFLAG sy-vline
WA_KGAL_ALL_STR-RCKUMUFLAG sy-vline
WA_KGAL_ALL_STR-PROC_GROUP sy-vline
WA_KGAL_ALL_STR-RKE_SPLIT sy-vline
WA_KGAL_ALL_STR-FM_DERIVE sy-vline
WA_KGAL_ALL_STR-GL_XBILK sy-vline
WA_KGAL_ALL_STR-FM_DERIVE_FONDS sy-vline
WA_KGAL_ALL_STR-FM_DERIVE_FKBER sy-vline
WA_KGAL_ALL_STR-FM_DERIVE_GRANT sy-vline
WA_KGAL_ALL_STR-FM_DERIVE_BUDPD sy-vline
WA_KGAL_ALL_STR-GL_LDGRP sy-vline
WA_KGAL_ALL_STR-PERIOD_LAG sy-vline
WA_KGAL_ALL_STR-PERIOD_DELTA sy-vline
WA_KGAL_ALL_STR-KALC_PRODPER sy-vline
WA_KGAL_ALL_STR-VARIABEL sy-vline
WA_KGAL_ALL_STR-SEQNR sy-vline
WA_KGAL_ALL_STR-NAME sy-vline
WA_KGAL_ALL_STR-SEQPOS sy-vline
WA_KGAL_ALL_STR-SRULE sy-vline
WA_KGAL_ALL_STR-RRULE sy-vline
WA_KGAL_ALL_STR-ACTIVE sy-vline
WA_KGAL_ALL_STR-NEGTEST sy-vline
WA_KGAL_ALL_STR-SPERCENT sy-vline
WA_KGAL_ALL_STR-SFFELD1 sy-vline
WA_KGAL_ALL_STR-SFFELD2 sy-vline
WA_KGAL_ALL_STR-SFFELD3 sy-vline
WA_KGAL_ALL_STR-SFFELD4 sy-vline
WA_KGAL_ALL_STR-SFFELD5 sy-vline
WA_KGAL_ALL_STR-SFFELD6 sy-vline
WA_KGAL_ALL_STR-SFFELD7 sy-vline
WA_KGAL_ALL_STR-SFFELD8 sy-vline
WA_KGAL_ALL_STR-SFFELD9 sy-vline
WA_KGAL_ALL_STR-SFFELD10 sy-vline
WA_KGAL_ALL_STR-FACURR sy-vline
WA_KGAL_ALL_STR-RFFELD1 sy-vline
WA_KGAL_ALL_STR-RFFELD2 sy-vline
WA_KGAL_ALL_STR-RFFELD3 sy-vline
WA_KGAL_ALL_STR-RFFELD4 sy-vline
WA_KGAL_ALL_STR-RFFELD5 sy-vline
WA_KGAL_ALL_STR-RFFELD6 sy-vline
WA_KGAL_ALL_STR-RFFELD7 sy-vline
WA_KGAL_ALL_STR-RFFELD8 sy-vline
WA_KGAL_ALL_STR-RFFELD9 sy-vline
WA_KGAL_ALL_STR-RFFELD10 sy-vline
WA_KGAL_ALL_STR-SCIP sy-vline
WA_KGAL_ALL_STR-RCDATA sy-vline
WA_KGAL_ALL_STR-ASACC sy-vline
WA_KGAL_ALL_STR-PAFLDF sy-vline
WA_KGAL_ALL_STR-PAFLDG sy-vline
WA_KGAL_ALL_STR-RRFELD1 sy-vline
WA_KGAL_ALL_STR-SORTFIELD sy-vline
WA_KGAL_ALL_STR-SENDER_NOT sy-vline
WA_KGAL_ALL_STR-MRULE sy-vline
WA_KGAL_ALL_STR-ABSCH sy-vline
WA_KGAL_ALL_STR-ERSCH sy-vline
WA_KGAL_ALL_STR-SWEIGHT_FACT sy-vline
WA_KGAL_ALL_STR-RWEIGHT_FACT sy-vline
WA_KGAL_ALL_STR-TXT sy-vline
WA_KGAL_ALL_STR-SETKIND sy-vline
WA_KGAL_ALL_STR-POS sy-vline
WA_KGAL_ALL_STR-FIELD sy-vline
WA_KGAL_ALL_STR-SETNAME sy-vline
WA_KGAL_ALL_STR-VALMIN sy-vline
WA_KGAL_ALL_STR-VALMAX sy-vline
WA_KGAL_ALL_STR-SETID sy-vline
WA_KGAL_ALL_STR-SRFLAG sy-vline
WA_KGAL_ALL_STR-FPOS sy-vline
WA_KGAL_ALL_STR-ELEMENT1 sy-vline
WA_KGAL_ALL_STR-ELEMENT2 sy-vline
WA_KGAL_ALL_STR-ELEMENT3 sy-vline
WA_KGAL_ALL_STR-ELEMENT4 sy-vline
WA_KGAL_ALL_STR-ELEMENT5 sy-vline
WA_KGAL_ALL_STR-ELEMENT6 sy-vline
WA_KGAL_ALL_STR-ELEMENT7 sy-vline
WA_KGAL_ALL_STR-ELEMENT8 sy-vline
WA_KGAL_ALL_STR-ELEMENT9 sy-vline
WA_KGAL_ALL_STR-ELEMENT10 sy-vline
WA_KGAL_ALL_STR-VALUE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.