ABAP Select data from SAP table PKMIT 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 PKMIT 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 PKMIT. 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 PKMIT 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_PKMIT TYPE STANDARD TABLE OF PKMIT,
      WA_PKMIT TYPE PKMIT,
      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: <PKMIT> TYPE PKMIT.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PKMIT
*  INTO TABLE @DATA(IT_PKMIT2).
*--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_PKMIT INDEX 1 INTO DATA(WA_PKMIT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PKMIT ASSIGNING <PKMIT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PKMIT>-MANDT = 1.
<PKMIT>-PLVAR = 1.
<PKMIT>-PLVTXT = 1.
<PKMIT>-LFCOD = 1.
<PKMIT>-LFCODT = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PKMIT-KMIT, sy-vline,
WA_PKMIT-KMTXT, sy-vline,
WA_PKMIT-FORML, sy-vline,
WA_PKMIT-FORMTXT, sy-vline,
WA_PKMIT-ADTYP, sy-vline,
WA_PKMIT-ADTXT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PKMIT 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_PKMIT 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_PKMIT INTO WA_PKMIT. *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 ISOLA, internal->external for field KRS1_LANGU CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_PKMIT-KRS1_LANGU IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PKMIT-KRS1_LANGU.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ISOLA, internal->external for field VOLNG CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_PKMIT-VOLNG IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PKMIT-VOLNG.
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_PKMIT_STR,
MANDT TYPE STRING,
PLVAR TYPE STRING,
PLVTXT TYPE STRING,
LFCOD TYPE STRING,
LFCODT TYPE STRING,
KMIT TYPE STRING,
KMTXT TYPE STRING,
FORML TYPE STRING,
FORMTXT TYPE STRING,
ADTYP TYPE STRING,
ADTXT TYPE STRING,
FNAME TYPE STRING,
DEVICE TYPE STRING,
DEV1 TYPE STRING,
DEV2 TYPE STRING,
DEV3 TYPE STRING,
DEV4 TYPE STRING,
DEV5 TYPE STRING,
DEV6 TYPE STRING,
DEV7 TYPE STRING,
DEV8 TYPE STRING,
PRINT TYPE STRING,
TYP_ID_SRK TYPE STRING,
TYP_ID TYPE STRING,
TYP_ISTAT TYPE STRING,
TYP_SHORT TYPE STRING,
TYP_STEXT TYPE STRING,
KRS1_ID_S TYPE STRING,
KRS1_ID TYPE STRING,
KRS1_ISTAT TYPE STRING,
KRS1_SHORT TYPE STRING,
KRS1_STEXT TYPE STRING,
KRS1_BEGDA TYPE STRING,
KRS1_ENDDA TYPE STRING,
KRS1_LANGU TYPE STRING,
KRS2_ID_S TYPE STRING,
KRS2_ID TYPE STRING,
KRS2_ISTAT TYPE STRING,
KRS2_SHORT TYPE STRING,
KRS2_STEXT TYPE STRING,
KRS2_BEGDA TYPE STRING,
KRS2_ENDDA TYPE STRING,
TEILNTYP TYPE STRING,
TEILNTYP_T TYPE STRING,
TEILN_ID_S TYPE STRING,
TEILN_ID TYPE STRING,
TEILN_SHOR TYPE STRING,
TEILN_TXT TYPE STRING,
ORGA_TYP TYPE STRING,
ORGA_TYP_T TYPE STRING,
ORGA_ID_S TYPE STRING,
ORGA_ID TYPE STRING,
ORGA_SHORT TYPE STRING,
ORGA_STEXT TYPE STRING,
CLANGU TYPE STRING,
VOBEG TYPE STRING,
VOEND TYPE STRING,
VOLNG TYPE STRING,
VOLTX TYPE STRING,
MANZL TYPE STRING,
PRIOX TYPE STRING,
BUDAT TYPE STRING,
OBLIG TYPE STRING,
NORMA TYPE STRING,
WAITL TYPE STRING,END OF T_EKKO_STR. DATA: WA_PKMIT_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_PKMIT_STR-MANDT sy-vline
WA_PKMIT_STR-PLVAR sy-vline
WA_PKMIT_STR-PLVTXT sy-vline
WA_PKMIT_STR-LFCOD sy-vline
WA_PKMIT_STR-LFCODT sy-vline
WA_PKMIT_STR-KMIT sy-vline
WA_PKMIT_STR-KMTXT sy-vline
WA_PKMIT_STR-FORML sy-vline
WA_PKMIT_STR-FORMTXT sy-vline
WA_PKMIT_STR-ADTYP sy-vline
WA_PKMIT_STR-ADTXT sy-vline
WA_PKMIT_STR-FNAME sy-vline
WA_PKMIT_STR-DEVICE sy-vline
WA_PKMIT_STR-DEV1 sy-vline
WA_PKMIT_STR-DEV2 sy-vline
WA_PKMIT_STR-DEV3 sy-vline
WA_PKMIT_STR-DEV4 sy-vline
WA_PKMIT_STR-DEV5 sy-vline
WA_PKMIT_STR-DEV6 sy-vline
WA_PKMIT_STR-DEV7 sy-vline
WA_PKMIT_STR-DEV8 sy-vline
WA_PKMIT_STR-PRINT sy-vline
WA_PKMIT_STR-TYP_ID_SRK sy-vline
WA_PKMIT_STR-TYP_ID sy-vline
WA_PKMIT_STR-TYP_ISTAT sy-vline
WA_PKMIT_STR-TYP_SHORT sy-vline
WA_PKMIT_STR-TYP_STEXT sy-vline
WA_PKMIT_STR-KRS1_ID_S sy-vline
WA_PKMIT_STR-KRS1_ID sy-vline
WA_PKMIT_STR-KRS1_ISTAT sy-vline
WA_PKMIT_STR-KRS1_SHORT sy-vline
WA_PKMIT_STR-KRS1_STEXT sy-vline
WA_PKMIT_STR-KRS1_BEGDA sy-vline
WA_PKMIT_STR-KRS1_ENDDA sy-vline
WA_PKMIT_STR-KRS1_LANGU sy-vline
WA_PKMIT_STR-KRS2_ID_S sy-vline
WA_PKMIT_STR-KRS2_ID sy-vline
WA_PKMIT_STR-KRS2_ISTAT sy-vline
WA_PKMIT_STR-KRS2_SHORT sy-vline
WA_PKMIT_STR-KRS2_STEXT sy-vline
WA_PKMIT_STR-KRS2_BEGDA sy-vline
WA_PKMIT_STR-KRS2_ENDDA sy-vline
WA_PKMIT_STR-TEILNTYP sy-vline
WA_PKMIT_STR-TEILNTYP_T sy-vline
WA_PKMIT_STR-TEILN_ID_S sy-vline
WA_PKMIT_STR-TEILN_ID sy-vline
WA_PKMIT_STR-TEILN_SHOR sy-vline
WA_PKMIT_STR-TEILN_TXT sy-vline
WA_PKMIT_STR-ORGA_TYP sy-vline
WA_PKMIT_STR-ORGA_TYP_T sy-vline
WA_PKMIT_STR-ORGA_ID_S sy-vline
WA_PKMIT_STR-ORGA_ID sy-vline
WA_PKMIT_STR-ORGA_SHORT sy-vline
WA_PKMIT_STR-ORGA_STEXT sy-vline
WA_PKMIT_STR-CLANGU sy-vline
WA_PKMIT_STR-VOBEG sy-vline
WA_PKMIT_STR-VOEND sy-vline
WA_PKMIT_STR-VOLNG sy-vline
WA_PKMIT_STR-VOLTX sy-vline
WA_PKMIT_STR-MANZL sy-vline
WA_PKMIT_STR-PRIOX sy-vline
WA_PKMIT_STR-BUDAT sy-vline
WA_PKMIT_STR-OBLIG sy-vline
WA_PKMIT_STR-NORMA sy-vline
WA_PKMIT_STR-WAITL sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.