ABAP Select data from SAP table PMEFD 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 PMEFD 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 PMEFD. 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 PMEFD 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_PMEFD TYPE STANDARD TABLE OF PMEFD,
      WA_PMEFD TYPE PMEFD,
      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: <PMEFD> TYPE PMEFD.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PMEFD
*  INTO TABLE @DATA(IT_PMEFD2).
*--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_PMEFD INDEX 1 INTO DATA(WA_PMEFD2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PMEFD ASSIGNING <PMEFD>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PMEFD>-DECRE = 1.
<PMEFD>-DATE = 1.
<PMEFD>-BUKRS = 1.
<PMEFD>-WERKS = 1.
<PMEFD>-BTRTL = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PMEFD-PERSG, sy-vline,
WA_PMEFD-PERSK, sy-vline,
WA_PMEFD-MOLGA, sy-vline,
WA_PMEFD-MASSN, sy-vline,
WA_PMEFD-MASSG, sy-vline,
WA_PMEFD-STELL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PMEFD 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_PMEFD 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_PMEFD INTO WA_PMEFD. *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 KOSTL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PMEFD-KOSTL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PMEFD-KOSTL.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field DD863_KOSTL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PMEFD-DD863_KOSTL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PMEFD-DD863_KOSTL.
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_PMEFD_STR,
DECRE TYPE STRING,
DATE TYPE STRING,
BUKRS TYPE STRING,
WERKS TYPE STRING,
BTRTL TYPE STRING,
PERSG TYPE STRING,
PERSK TYPE STRING,
MOLGA TYPE STRING,
MASSN TYPE STRING,
MASSG TYPE STRING,
STELL TYPE STRING,
ANSVH TYPE STRING,
ORGEH TYPE STRING,
PLANS TYPE STRING,
KOSTL TYPE STRING,
CTTYP TYPE STRING,
TRFAR TYPE STRING,
TRFGB TYPE STRING,
TRFGR TYPE STRING,
TRFST TYPE STRING,
TEILK TYPE STRING,
BSGRD TYPE STRING,
TPART TYPE STRING,
REGSS TYPE STRING,
REGAC TYPE STRING,
REGRT TYPE STRING,
REGNO TYPE STRING,
SCHKZ TYPE STRING,
ZTERF TYPE STRING,
EMPCT TYPE STRING,
ARBST TYPE STRING,
WOSTD TYPE STRING,
MOSTD TYPE STRING,
WKWDY TYPE STRING,
PERNR TYPE STRING,
KZTIM TYPE STRING,
CPPSD TYPE STRING,
PSSTT TYPE STRING,
PSADM TYPE STRING,
ORGAN TYPE STRING,
CORPS TYPE STRING,
GRADE TYPE STRING,
DIVGV TYPE STRING,
COLPH TYPE STRING,
CTFTE TYPE STRING,
DD863_BUKRS TYPE STRING,
DD863_WERKS TYPE STRING,
DD863_BTRTL TYPE STRING,
DD863_PERSG TYPE STRING,
DD863_PERSK TYPE STRING,
DD863_MOLGA TYPE STRING,
DD863_MASSN TYPE STRING,
DD863_MASSG TYPE STRING,
DD863_STELL TYPE STRING,
DD863_ANSVH TYPE STRING,
DD863_ORGEH TYPE STRING,
DD863_PLANS TYPE STRING,
DD863_KOSTL TYPE STRING,
DD863_CTTYP TYPE STRING,
DD863_TRFAR TYPE STRING,
DD863_TRFGB TYPE STRING,
DD863_TRFGR TYPE STRING,
DD863_TRFST TYPE STRING,
DD863_TEILK TYPE STRING,
DD863_BSGRD TYPE STRING,
DD863_TPART TYPE STRING,
DD863_REGSS TYPE STRING,
DD863_REGAC TYPE STRING,
DD863_REGRT TYPE STRING,
DD863_REGN0 TYPE STRING,
DD863_SCHKZ TYPE STRING,
DD863_ZTERF TYPE STRING,
DD863_EMPCT TYPE STRING,
DD863_ARBST TYPE STRING,
DD863_WOSTD TYPE STRING,
DD863_MOSTD TYPE STRING,
DD863_WKWDY TYPE STRING,
DD863_PERNR TYPE STRING,
DD863_KZTIM TYPE STRING,
DD863_CPPSD TYPE STRING,
DD863_PSSTT TYPE STRING,
DD863_PSADM TYPE STRING,
DD863_ORGAN TYPE STRING,
DD863_CORPS TYPE STRING,
DD863_GRADE TYPE STRING,
NATUR TYPE STRING,END OF T_EKKO_STR. DATA: WA_PMEFD_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_PMEFD_STR-DECRE sy-vline
WA_PMEFD_STR-DATE sy-vline
WA_PMEFD_STR-BUKRS sy-vline
WA_PMEFD_STR-WERKS sy-vline
WA_PMEFD_STR-BTRTL sy-vline
WA_PMEFD_STR-PERSG sy-vline
WA_PMEFD_STR-PERSK sy-vline
WA_PMEFD_STR-MOLGA sy-vline
WA_PMEFD_STR-MASSN sy-vline
WA_PMEFD_STR-MASSG sy-vline
WA_PMEFD_STR-STELL sy-vline
WA_PMEFD_STR-ANSVH sy-vline
WA_PMEFD_STR-ORGEH sy-vline
WA_PMEFD_STR-PLANS sy-vline
WA_PMEFD_STR-KOSTL sy-vline
WA_PMEFD_STR-CTTYP sy-vline
WA_PMEFD_STR-TRFAR sy-vline
WA_PMEFD_STR-TRFGB sy-vline
WA_PMEFD_STR-TRFGR sy-vline
WA_PMEFD_STR-TRFST sy-vline
WA_PMEFD_STR-TEILK sy-vline
WA_PMEFD_STR-BSGRD sy-vline
WA_PMEFD_STR-TPART sy-vline
WA_PMEFD_STR-REGSS sy-vline
WA_PMEFD_STR-REGAC sy-vline
WA_PMEFD_STR-REGRT sy-vline
WA_PMEFD_STR-REGNO sy-vline
WA_PMEFD_STR-SCHKZ sy-vline
WA_PMEFD_STR-ZTERF sy-vline
WA_PMEFD_STR-EMPCT sy-vline
WA_PMEFD_STR-ARBST sy-vline
WA_PMEFD_STR-WOSTD sy-vline
WA_PMEFD_STR-MOSTD sy-vline
WA_PMEFD_STR-WKWDY sy-vline
WA_PMEFD_STR-PERNR sy-vline
WA_PMEFD_STR-KZTIM sy-vline
WA_PMEFD_STR-CPPSD sy-vline
WA_PMEFD_STR-PSSTT sy-vline
WA_PMEFD_STR-PSADM sy-vline
WA_PMEFD_STR-ORGAN sy-vline
WA_PMEFD_STR-CORPS sy-vline
WA_PMEFD_STR-GRADE sy-vline
WA_PMEFD_STR-DIVGV sy-vline
WA_PMEFD_STR-COLPH sy-vline
WA_PMEFD_STR-CTFTE sy-vline
WA_PMEFD_STR-DD863_BUKRS sy-vline
WA_PMEFD_STR-DD863_WERKS sy-vline
WA_PMEFD_STR-DD863_BTRTL sy-vline
WA_PMEFD_STR-DD863_PERSG sy-vline
WA_PMEFD_STR-DD863_PERSK sy-vline
WA_PMEFD_STR-DD863_MOLGA sy-vline
WA_PMEFD_STR-DD863_MASSN sy-vline
WA_PMEFD_STR-DD863_MASSG sy-vline
WA_PMEFD_STR-DD863_STELL sy-vline
WA_PMEFD_STR-DD863_ANSVH sy-vline
WA_PMEFD_STR-DD863_ORGEH sy-vline
WA_PMEFD_STR-DD863_PLANS sy-vline
WA_PMEFD_STR-DD863_KOSTL sy-vline
WA_PMEFD_STR-DD863_CTTYP sy-vline
WA_PMEFD_STR-DD863_TRFAR sy-vline
WA_PMEFD_STR-DD863_TRFGB sy-vline
WA_PMEFD_STR-DD863_TRFGR sy-vline
WA_PMEFD_STR-DD863_TRFST sy-vline
WA_PMEFD_STR-DD863_TEILK sy-vline
WA_PMEFD_STR-DD863_BSGRD sy-vline
WA_PMEFD_STR-DD863_TPART sy-vline
WA_PMEFD_STR-DD863_REGSS sy-vline
WA_PMEFD_STR-DD863_REGAC sy-vline
WA_PMEFD_STR-DD863_REGRT sy-vline
WA_PMEFD_STR-DD863_REGN0 sy-vline
WA_PMEFD_STR-DD863_SCHKZ sy-vline
WA_PMEFD_STR-DD863_ZTERF sy-vline
WA_PMEFD_STR-DD863_EMPCT sy-vline
WA_PMEFD_STR-DD863_ARBST sy-vline
WA_PMEFD_STR-DD863_WOSTD sy-vline
WA_PMEFD_STR-DD863_MOSTD sy-vline
WA_PMEFD_STR-DD863_WKWDY sy-vline
WA_PMEFD_STR-DD863_PERNR sy-vline
WA_PMEFD_STR-DD863_KZTIM sy-vline
WA_PMEFD_STR-DD863_CPPSD sy-vline
WA_PMEFD_STR-DD863_PSSTT sy-vline
WA_PMEFD_STR-DD863_PSADM sy-vline
WA_PMEFD_STR-DD863_ORGAN sy-vline
WA_PMEFD_STR-DD863_CORPS sy-vline
WA_PMEFD_STR-DD863_GRADE sy-vline
WA_PMEFD_STR-NATUR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.