ABAP Select data from SAP table M047X 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 M047X 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 M047X. 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 M047X 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_M047X TYPE STANDARD TABLE OF M047X,
      WA_M047X TYPE M047X,
      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: <M047X> TYPE M047X.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM M047X
*  INTO TABLE @DATA(IT_M047X2).
*--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_M047X INDEX 1 INTO DATA(WA_M047X2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_M047X ASSIGNING <M047X>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<M047X>-XNEIN = 1.
<M047X>-XJA = 1.
<M047X>-MAXST = 1.
<M047X>-VERT1 = 1.
<M047X>-XZIN1 = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_M047X-XPOS1, sy-vline,
WA_M047X-XAEN1, sy-vline,
WA_M047X-VERT2, sy-vline,
WA_M047X-XZIN2, sy-vline,
WA_M047X-XPOS2, sy-vline,
WA_M047X-XAEN2, sy-vline.
ENDLOOP. *Add any further fields from structure WA_M047X 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_M047X 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_M047X INTO WA_M047X. *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.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_M047X_STR,
XNEIN TYPE STRING,
XJA TYPE STRING,
MAXST TYPE STRING,
VERT1 TYPE STRING,
XZIN1 TYPE STRING,
XPOS1 TYPE STRING,
XAEN1 TYPE STRING,
VERT2 TYPE STRING,
XZIN2 TYPE STRING,
XPOS2 TYPE STRING,
XAEN2 TYPE STRING,
VERT3 TYPE STRING,
XZIN3 TYPE STRING,
XPOS3 TYPE STRING,
XAEN3 TYPE STRING,
VERT4 TYPE STRING,
XZIN4 TYPE STRING,
XPOS4 TYPE STRING,
XAEN4 TYPE STRING,
VERT5 TYPE STRING,
XZIN5 TYPE STRING,
XPOS5 TYPE STRING,
XAEN5 TYPE STRING,
VERT6 TYPE STRING,
XZIN6 TYPE STRING,
XPOS6 TYPE STRING,
XAEN6 TYPE STRING,
VERT7 TYPE STRING,
XZIN7 TYPE STRING,
XPOS7 TYPE STRING,
XAEN7 TYPE STRING,
VERT8 TYPE STRING,
XZIN8 TYPE STRING,
XPOS8 TYPE STRING,
XAEN8 TYPE STRING,
VERT9 TYPE STRING,
XZIN9 TYPE STRING,
XPOS9 TYPE STRING,
XAEN9 TYPE STRING,
WAERS TYPE STRING,
WAER1 TYPE STRING,
BUKRS TYPE STRING,
KOART TYPE STRING,
UMSK1 TYPE STRING,
TEXT1 TYPE STRING,
UMSK2 TYPE STRING,
TEXT2 TYPE STRING,
XSUMS TYPE STRING,
XFLD1 TYPE STRING,
XFLD2 TYPE STRING,
XDEBI TYPE STRING,
XKRED TYPE STRING,
JAPOS1 TYPE STRING,
ATAP1 TYPE STRING,
JAPOS2 TYPE STRING,
ATAP2 TYPE STRING,
JAPOS3 TYPE STRING,
ATAP3 TYPE STRING,
JAPOS4 TYPE STRING,
ATAP4 TYPE STRING,
JAPOS5 TYPE STRING,
ATAP5 TYPE STRING,
JAPOS6 TYPE STRING,
ATAP6 TYPE STRING,
JAPOS7 TYPE STRING,
ATAP7 TYPE STRING,
JAPOS8 TYPE STRING,
ATAP8 TYPE STRING,
JAPOS9 TYPE STRING,
ATAP9 TYPE STRING,END OF T_EKKO_STR. DATA: WA_M047X_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_M047X_STR-XNEIN sy-vline
WA_M047X_STR-XJA sy-vline
WA_M047X_STR-MAXST sy-vline
WA_M047X_STR-VERT1 sy-vline
WA_M047X_STR-XZIN1 sy-vline
WA_M047X_STR-XPOS1 sy-vline
WA_M047X_STR-XAEN1 sy-vline
WA_M047X_STR-VERT2 sy-vline
WA_M047X_STR-XZIN2 sy-vline
WA_M047X_STR-XPOS2 sy-vline
WA_M047X_STR-XAEN2 sy-vline
WA_M047X_STR-VERT3 sy-vline
WA_M047X_STR-XZIN3 sy-vline
WA_M047X_STR-XPOS3 sy-vline
WA_M047X_STR-XAEN3 sy-vline
WA_M047X_STR-VERT4 sy-vline
WA_M047X_STR-XZIN4 sy-vline
WA_M047X_STR-XPOS4 sy-vline
WA_M047X_STR-XAEN4 sy-vline
WA_M047X_STR-VERT5 sy-vline
WA_M047X_STR-XZIN5 sy-vline
WA_M047X_STR-XPOS5 sy-vline
WA_M047X_STR-XAEN5 sy-vline
WA_M047X_STR-VERT6 sy-vline
WA_M047X_STR-XZIN6 sy-vline
WA_M047X_STR-XPOS6 sy-vline
WA_M047X_STR-XAEN6 sy-vline
WA_M047X_STR-VERT7 sy-vline
WA_M047X_STR-XZIN7 sy-vline
WA_M047X_STR-XPOS7 sy-vline
WA_M047X_STR-XAEN7 sy-vline
WA_M047X_STR-VERT8 sy-vline
WA_M047X_STR-XZIN8 sy-vline
WA_M047X_STR-XPOS8 sy-vline
WA_M047X_STR-XAEN8 sy-vline
WA_M047X_STR-VERT9 sy-vline
WA_M047X_STR-XZIN9 sy-vline
WA_M047X_STR-XPOS9 sy-vline
WA_M047X_STR-XAEN9 sy-vline
WA_M047X_STR-WAERS sy-vline
WA_M047X_STR-WAER1 sy-vline
WA_M047X_STR-BUKRS sy-vline
WA_M047X_STR-KOART sy-vline
WA_M047X_STR-UMSK1 sy-vline
WA_M047X_STR-TEXT1 sy-vline
WA_M047X_STR-UMSK2 sy-vline
WA_M047X_STR-TEXT2 sy-vline
WA_M047X_STR-XSUMS sy-vline
WA_M047X_STR-XFLD1 sy-vline
WA_M047X_STR-XFLD2 sy-vline
WA_M047X_STR-XDEBI sy-vline
WA_M047X_STR-XKRED sy-vline
WA_M047X_STR-JAPOS1 sy-vline
WA_M047X_STR-ATAP1 sy-vline
WA_M047X_STR-JAPOS2 sy-vline
WA_M047X_STR-ATAP2 sy-vline
WA_M047X_STR-JAPOS3 sy-vline
WA_M047X_STR-ATAP3 sy-vline
WA_M047X_STR-JAPOS4 sy-vline
WA_M047X_STR-ATAP4 sy-vline
WA_M047X_STR-JAPOS5 sy-vline
WA_M047X_STR-ATAP5 sy-vline
WA_M047X_STR-JAPOS6 sy-vline
WA_M047X_STR-ATAP6 sy-vline
WA_M047X_STR-JAPOS7 sy-vline
WA_M047X_STR-ATAP7 sy-vline
WA_M047X_STR-JAPOS8 sy-vline
WA_M047X_STR-ATAP8 sy-vline
WA_M047X_STR-JAPOS9 sy-vline
WA_M047X_STR-ATAP9 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.