ABAP Select data from SAP table /SPE/MBEW_PLANT 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 /SPE/MBEW_PLANT 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 /SPE/MBEW_PLANT. 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 /SPE/MBEW_PLANT 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_/SPE/MBEW_PLANT TYPE STANDARD TABLE OF /SPE/MBEW_PLANT,
      WA_/SPE/MBEW_PLANT TYPE /SPE/MBEW_PLANT,
      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: </SPE/MBEW_PLANT> TYPE /SPE/MBEW_PLANT.

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

SELECT *
*restrict ABAP select to first 10 rows
 UP TO 10 ROWS      
  FROM /SPE/MBEW_PLANT
  INTO TABLE IT_/SPE/MBEW_PLANT.

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM /SPE/MBEW_PLANT
*  INTO TABLE @DATA(IT_/SPE/MBEW_PLANT2).
*--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_/SPE/MBEW_PLANT INDEX 1 INTO DATA(WA_/SPE/MBEW_PLANT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_/SPE/MBEW_PLANT ASSIGNING </SPE/MBEW_PLANT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
</SPE/MBEW_PLANT>-MANDT = 1.
</SPE/MBEW_PLANT>-MATNR = 1.
</SPE/MBEW_PLANT>-BWKEY = 1.
</SPE/MBEW_PLANT>-BWTAR = 1.
</SPE/MBEW_PLANT>-LVORM = 1.
ENDLOOP.

LOOP AT IT_/SPE/MBEW_PLANT INTO WA_/SPE/MBEW_PLANT.
*Write horizonal line to screen report.
  WRITE:/ sy-uline.

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_/SPE/MBEW_PLANT-LBKUM, sy-vline,
WA_/SPE/MBEW_PLANT-SALK3, sy-vline,
WA_/SPE/MBEW_PLANT-VPRSV, sy-vline,
WA_/SPE/MBEW_PLANT-VERPR, sy-vline,
WA_/SPE/MBEW_PLANT-STPRS, sy-vline,
WA_/SPE/MBEW_PLANT-PEINH, sy-vline.
ENDLOOP. *Add any further fields from structure WA_/SPE/MBEW_PLANT 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_/SPE/MBEW_PLANT 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_/SPE/MBEW_PLANT INTO WA_/SPE/MBEW_PLANT. *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 MATN1, internal->external for field MATNR CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-MATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-MATNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field LFGJA CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-LFGJA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-LFGJA.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field PDATZ CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-PDATZ IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-PDATZ.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field PDATL CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-PDATL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-PDATL.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field PDATV CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-PDATV IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-PDATV.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATNL, internal->external for field MATNR_EXT CALL FUNCTION 'CONVERSION_EXIT_MATNL_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-MATNR_EXT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-MATNR_EXT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATNW, internal->external for field MATNR_VERS CALL FUNCTION 'CONVERSION_EXIT_MATNW_OUTPUT' EXPORTING input = WA_/SPE/MBEW_PLANT-MATNR_VERS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_/SPE/MBEW_PLANT-MATNR_VERS.
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_/SPE/MBEW_PLANT_STR,
MANDT TYPE STRING,
MATNR TYPE STRING,
BWKEY TYPE STRING,
BWTAR TYPE STRING,
LVORM TYPE STRING,
LBKUM TYPE STRING,
SALK3 TYPE STRING,
VPRSV TYPE STRING,
VERPR TYPE STRING,
STPRS TYPE STRING,
PEINH TYPE STRING,
BKLAS TYPE STRING,
SALKV TYPE STRING,
VMKUM TYPE STRING,
VMSAL TYPE STRING,
VMVPR TYPE STRING,
VMVER TYPE STRING,
VMSTP TYPE STRING,
VMPEI TYPE STRING,
VMBKL TYPE STRING,
VMSAV TYPE STRING,
VJKUM TYPE STRING,
VJSAL TYPE STRING,
VJVPR TYPE STRING,
VJVER TYPE STRING,
VJSTP TYPE STRING,
VJPEI TYPE STRING,
VJBKL TYPE STRING,
VJSAV TYPE STRING,
LFGJA TYPE STRING,
LFMON TYPE STRING,
BWTTY TYPE STRING,
STPRV TYPE STRING,
LAEPR TYPE STRING,
ZKPRS TYPE STRING,
ZKDAT TYPE STRING,
TIMESTAMP TYPE STRING,
BWPRS TYPE STRING,
BWPRH TYPE STRING,
VJBWS TYPE STRING,
VJBWH TYPE STRING,
VVJSL TYPE STRING,
VVJLB TYPE STRING,
VVMLB TYPE STRING,
VVSAL TYPE STRING,
ZPLPR TYPE STRING,
ZPLP1 TYPE STRING,
ZPLP2 TYPE STRING,
ZPLP3 TYPE STRING,
ZPLD1 TYPE STRING,
ZPLD2 TYPE STRING,
ZPLD3 TYPE STRING,
PPERZ TYPE STRING,
PPERL TYPE STRING,
PPERV TYPE STRING,
KALKZ TYPE STRING,
KALKL TYPE STRING,
KALKV TYPE STRING,
KALSC TYPE STRING,
XLIFO TYPE STRING,
MYPOL TYPE STRING,
BWPH1 TYPE STRING,
BWPS1 TYPE STRING,
ABWKZ TYPE STRING,
PSTAT TYPE STRING,
KALN1 TYPE STRING,
KALNR TYPE STRING,
BWVA1 TYPE STRING,
BWVA2 TYPE STRING,
BWVA3 TYPE STRING,
VERS1 TYPE STRING,
VERS2 TYPE STRING,
VERS3 TYPE STRING,
HRKFT TYPE STRING,
KOSGR TYPE STRING,
PPRDZ TYPE STRING,
PPRDL TYPE STRING,
PPRDV TYPE STRING,
PDATZ TYPE STRING,
PDATL TYPE STRING,
PDATV TYPE STRING,
EKALR TYPE STRING,
VPLPR TYPE STRING,
MLMAA TYPE STRING,
MLAST TYPE STRING,
LPLPR TYPE STRING,
VKSAL TYPE STRING,
HKMAT TYPE STRING,
SPERW TYPE STRING,
KZIWL TYPE STRING,
WLINL TYPE STRING,
ABCIW TYPE STRING,
BWSPA TYPE STRING,
LPLPX TYPE STRING,
VPLPX TYPE STRING,
FPLPX TYPE STRING,
LBWST TYPE STRING,
VBWST TYPE STRING,
FBWST TYPE STRING,
EKLAS TYPE STRING,
QKLAS TYPE STRING,
MTUSE TYPE STRING,
MTORG TYPE STRING,
OWNPR TYPE STRING,
XBEWM TYPE STRING,
BWPEI TYPE STRING,
MBRUE TYPE STRING,
OKLAS TYPE STRING,
DUMMY_VAL_INCL_EEW_PS TYPE STRING,
OIPPINV TYPE STRING,
PLANT TYPE STRING,
MATNR_EXT TYPE STRING,
MATNR_GUID TYPE STRING,
MATNR_VERS TYPE STRING,
WAERS TYPE STRING,END OF T_EKKO_STR. DATA: WA_/SPE/MBEW_PLANT_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_/SPE/MBEW_PLANT_STR-MANDT sy-vline
WA_/SPE/MBEW_PLANT_STR-MATNR sy-vline
WA_/SPE/MBEW_PLANT_STR-BWKEY sy-vline
WA_/SPE/MBEW_PLANT_STR-BWTAR sy-vline
WA_/SPE/MBEW_PLANT_STR-LVORM sy-vline
WA_/SPE/MBEW_PLANT_STR-LBKUM sy-vline
WA_/SPE/MBEW_PLANT_STR-SALK3 sy-vline
WA_/SPE/MBEW_PLANT_STR-VPRSV sy-vline
WA_/SPE/MBEW_PLANT_STR-VERPR sy-vline
WA_/SPE/MBEW_PLANT_STR-STPRS sy-vline
WA_/SPE/MBEW_PLANT_STR-PEINH sy-vline
WA_/SPE/MBEW_PLANT_STR-BKLAS sy-vline
WA_/SPE/MBEW_PLANT_STR-SALKV sy-vline
WA_/SPE/MBEW_PLANT_STR-VMKUM sy-vline
WA_/SPE/MBEW_PLANT_STR-VMSAL sy-vline
WA_/SPE/MBEW_PLANT_STR-VMVPR sy-vline
WA_/SPE/MBEW_PLANT_STR-VMVER sy-vline
WA_/SPE/MBEW_PLANT_STR-VMSTP sy-vline
WA_/SPE/MBEW_PLANT_STR-VMPEI sy-vline
WA_/SPE/MBEW_PLANT_STR-VMBKL sy-vline
WA_/SPE/MBEW_PLANT_STR-VMSAV sy-vline
WA_/SPE/MBEW_PLANT_STR-VJKUM sy-vline
WA_/SPE/MBEW_PLANT_STR-VJSAL sy-vline
WA_/SPE/MBEW_PLANT_STR-VJVPR sy-vline
WA_/SPE/MBEW_PLANT_STR-VJVER sy-vline
WA_/SPE/MBEW_PLANT_STR-VJSTP sy-vline
WA_/SPE/MBEW_PLANT_STR-VJPEI sy-vline
WA_/SPE/MBEW_PLANT_STR-VJBKL sy-vline
WA_/SPE/MBEW_PLANT_STR-VJSAV sy-vline
WA_/SPE/MBEW_PLANT_STR-LFGJA sy-vline
WA_/SPE/MBEW_PLANT_STR-LFMON sy-vline
WA_/SPE/MBEW_PLANT_STR-BWTTY sy-vline
WA_/SPE/MBEW_PLANT_STR-STPRV sy-vline
WA_/SPE/MBEW_PLANT_STR-LAEPR sy-vline
WA_/SPE/MBEW_PLANT_STR-ZKPRS sy-vline
WA_/SPE/MBEW_PLANT_STR-ZKDAT sy-vline
WA_/SPE/MBEW_PLANT_STR-TIMESTAMP sy-vline
WA_/SPE/MBEW_PLANT_STR-BWPRS sy-vline
WA_/SPE/MBEW_PLANT_STR-BWPRH sy-vline
WA_/SPE/MBEW_PLANT_STR-VJBWS sy-vline
WA_/SPE/MBEW_PLANT_STR-VJBWH sy-vline
WA_/SPE/MBEW_PLANT_STR-VVJSL sy-vline
WA_/SPE/MBEW_PLANT_STR-VVJLB sy-vline
WA_/SPE/MBEW_PLANT_STR-VVMLB sy-vline
WA_/SPE/MBEW_PLANT_STR-VVSAL sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLPR sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLP1 sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLP2 sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLP3 sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLD1 sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLD2 sy-vline
WA_/SPE/MBEW_PLANT_STR-ZPLD3 sy-vline
WA_/SPE/MBEW_PLANT_STR-PPERZ sy-vline
WA_/SPE/MBEW_PLANT_STR-PPERL sy-vline
WA_/SPE/MBEW_PLANT_STR-PPERV sy-vline
WA_/SPE/MBEW_PLANT_STR-KALKZ sy-vline
WA_/SPE/MBEW_PLANT_STR-KALKL sy-vline
WA_/SPE/MBEW_PLANT_STR-KALKV sy-vline
WA_/SPE/MBEW_PLANT_STR-KALSC sy-vline
WA_/SPE/MBEW_PLANT_STR-XLIFO sy-vline
WA_/SPE/MBEW_PLANT_STR-MYPOL sy-vline
WA_/SPE/MBEW_PLANT_STR-BWPH1 sy-vline
WA_/SPE/MBEW_PLANT_STR-BWPS1 sy-vline
WA_/SPE/MBEW_PLANT_STR-ABWKZ sy-vline
WA_/SPE/MBEW_PLANT_STR-PSTAT sy-vline
WA_/SPE/MBEW_PLANT_STR-KALN1 sy-vline
WA_/SPE/MBEW_PLANT_STR-KALNR sy-vline
WA_/SPE/MBEW_PLANT_STR-BWVA1 sy-vline
WA_/SPE/MBEW_PLANT_STR-BWVA2 sy-vline
WA_/SPE/MBEW_PLANT_STR-BWVA3 sy-vline
WA_/SPE/MBEW_PLANT_STR-VERS1 sy-vline
WA_/SPE/MBEW_PLANT_STR-VERS2 sy-vline
WA_/SPE/MBEW_PLANT_STR-VERS3 sy-vline
WA_/SPE/MBEW_PLANT_STR-HRKFT sy-vline
WA_/SPE/MBEW_PLANT_STR-KOSGR sy-vline
WA_/SPE/MBEW_PLANT_STR-PPRDZ sy-vline
WA_/SPE/MBEW_PLANT_STR-PPRDL sy-vline
WA_/SPE/MBEW_PLANT_STR-PPRDV sy-vline
WA_/SPE/MBEW_PLANT_STR-PDATZ sy-vline
WA_/SPE/MBEW_PLANT_STR-PDATL sy-vline
WA_/SPE/MBEW_PLANT_STR-PDATV sy-vline
WA_/SPE/MBEW_PLANT_STR-EKALR sy-vline
WA_/SPE/MBEW_PLANT_STR-VPLPR sy-vline
WA_/SPE/MBEW_PLANT_STR-MLMAA sy-vline
WA_/SPE/MBEW_PLANT_STR-MLAST sy-vline
WA_/SPE/MBEW_PLANT_STR-LPLPR sy-vline
WA_/SPE/MBEW_PLANT_STR-VKSAL sy-vline
WA_/SPE/MBEW_PLANT_STR-HKMAT sy-vline
WA_/SPE/MBEW_PLANT_STR-SPERW sy-vline
WA_/SPE/MBEW_PLANT_STR-KZIWL sy-vline
WA_/SPE/MBEW_PLANT_STR-WLINL sy-vline
WA_/SPE/MBEW_PLANT_STR-ABCIW sy-vline
WA_/SPE/MBEW_PLANT_STR-BWSPA sy-vline
WA_/SPE/MBEW_PLANT_STR-LPLPX sy-vline
WA_/SPE/MBEW_PLANT_STR-VPLPX sy-vline
WA_/SPE/MBEW_PLANT_STR-FPLPX sy-vline
WA_/SPE/MBEW_PLANT_STR-LBWST sy-vline
WA_/SPE/MBEW_PLANT_STR-VBWST sy-vline
WA_/SPE/MBEW_PLANT_STR-FBWST sy-vline
WA_/SPE/MBEW_PLANT_STR-EKLAS sy-vline
WA_/SPE/MBEW_PLANT_STR-QKLAS sy-vline
WA_/SPE/MBEW_PLANT_STR-MTUSE sy-vline
WA_/SPE/MBEW_PLANT_STR-MTORG sy-vline
WA_/SPE/MBEW_PLANT_STR-OWNPR sy-vline
WA_/SPE/MBEW_PLANT_STR-XBEWM sy-vline
WA_/SPE/MBEW_PLANT_STR-BWPEI sy-vline
WA_/SPE/MBEW_PLANT_STR-MBRUE sy-vline
WA_/SPE/MBEW_PLANT_STR-OKLAS sy-vline
WA_/SPE/MBEW_PLANT_STR-DUMMY_VAL_INCL_EEW_PS sy-vline
WA_/SPE/MBEW_PLANT_STR-OIPPINV sy-vline
WA_/SPE/MBEW_PLANT_STR-PLANT sy-vline
WA_/SPE/MBEW_PLANT_STR-MATNR_EXT sy-vline
WA_/SPE/MBEW_PLANT_STR-MATNR_GUID sy-vline
WA_/SPE/MBEW_PLANT_STR-MATNR_VERS sy-vline
WA_/SPE/MBEW_PLANT_STR-WAERS sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.