ABAP Select data from SAP table MTCOR 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 MTCOR 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 MTCOR. 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 MTCOR 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_MTCOR TYPE STANDARD TABLE OF MTCOR, WA_MTCOR TYPE MTCOR, 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: <MTCOR> TYPE MTCOR. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_MTCOR. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM MTCOR INTO TABLE IT_MTCOR. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM MTCOR * INTO TABLE @DATA(IT_MTCOR2). *--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_MTCOR INDEX 1 INTO DATA(WA_MTCOR2). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_MTCOR ASSIGNING <MTCOR>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<MTCOR>-RMAR1 = 1.
<MTCOR>-RMARA = 1.
<MTCOR>-RMAKT = 1.
<MTCOR>-RMARC = 1.
<MTCOR>-RMARD = 1.
ENDLOOP. LOOP AT IT_MTCOR INTO WA_MTCOR. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_MTCOR-RMCHA, sy-vline,
WA_MTCOR-RMCHB, sy-vline,
WA_MTCOR-RMCH1, sy-vline,
WA_MTCOR-RMBEW, sy-vline,
WA_MTCOR-RMVKE, sy-vline,
WA_MTCOR-RMLGN, sy-vline.
ENDLOOP. *Add any further fields from structure WA_MTCOR 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_MTCOR 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_MTCOR INTO WA_MTCOR. *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_MTCOR_STR,
RMAR1 TYPE STRING,
RMARA TYPE STRING,
RMAKT TYPE STRING,
RMARC TYPE STRING,
RMARD TYPE STRING,
RMCHA TYPE STRING,
RMCHB TYPE STRING,
RMCH1 TYPE STRING,
RMBEW TYPE STRING,
RMVKE TYPE STRING,
RMLGN TYPE STRING,
RMLGT TYPE STRING,
LVORM TYPE STRING,
FSTAT TYPE STRING,
PSTAT TYPE STRING,
RT134 TYPE STRING,
RT134W TYPE STRING,
RV134W TYPE STRING,
RT134M TYPE STRING,
RT405 TYPE STRING,
RT438A TYPE STRING,
RMKOP TYPE STRING,
RMKOL TYPE STRING,
RMLAN TYPE STRING,
RMAPOV TYPE STRING,
RMAWEV TYPE STRING,
RT149 TYPE STRING,
RT134G TYPE STRING,
RV134G TYPE STRING,
RT134H TYPE STRING,
RMA07V TYPE STRING,
RMB61V TYPE STRING,
RMB06V TYPE STRING,
RMSLB TYPE STRING,
RMSKU TYPE STRING,
RMSKB TYPE STRING,
RMSKA TYPE STRING,
RMSPR TYPE STRING,
RMSSE TYPE STRING,
RMSSL TYPE STRING,
RMSSK TYPE STRING,
RMSSA TYPE STRING,
RMSSQ TYPE STRING,
RMSSS TYPE STRING,
RMSCA TYPE STRING,
RMSCP TYPE STRING,
RMSCS TYPE STRING,
RMSCB TYPE STRING,
RMSOA TYPE STRING,
RMSOP TYPE STRING,
RMSOS TYPE STRING,
RMSOB TYPE STRING,
RMKAL TYPE STRING,
RMFHM TYPE STRING,
RMYMS TYPE STRING,
RMPOP TYPE STRING,
RMSFCD TYPE STRING,
SAMAT TYPE STRING,
WVMAT TYPE STRING,
LVOCA TYPE STRING,
LVOCB TYPE STRING,
REBEW TYPE STRING,
RQBEW TYPE STRING,
MPN_LVORM TYPE STRING,
ROBEW TYPE STRING,
RMDMA TYPE STRING,
RMSTB TYPE STRING,
RMSTE TYPE STRING,
RMSTQ TYPE STRING,
RMCSD TYPE STRING,
RMCSS TYPE STRING,
RMSCD TYPE STRING,
RMSFD TYPE STRING,
RMSFS TYPE STRING,
RMSID TYPE STRING,
RMSIS TYPE STRING,
RMSRD TYPE STRING,
RMSRS TYPE STRING,
IND_HEAD TYPE STRING,
IND_INVALID TYPE STRING,END OF T_EKKO_STR. DATA: WA_MTCOR_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_MTCOR_STR-RMAR1 sy-vline
WA_MTCOR_STR-RMARA sy-vline
WA_MTCOR_STR-RMAKT sy-vline
WA_MTCOR_STR-RMARC sy-vline
WA_MTCOR_STR-RMARD sy-vline
WA_MTCOR_STR-RMCHA sy-vline
WA_MTCOR_STR-RMCHB sy-vline
WA_MTCOR_STR-RMCH1 sy-vline
WA_MTCOR_STR-RMBEW sy-vline
WA_MTCOR_STR-RMVKE sy-vline
WA_MTCOR_STR-RMLGN sy-vline
WA_MTCOR_STR-RMLGT sy-vline
WA_MTCOR_STR-LVORM sy-vline
WA_MTCOR_STR-FSTAT sy-vline
WA_MTCOR_STR-PSTAT sy-vline
WA_MTCOR_STR-RT134 sy-vline
WA_MTCOR_STR-RT134W sy-vline
WA_MTCOR_STR-RV134W sy-vline
WA_MTCOR_STR-RT134M sy-vline
WA_MTCOR_STR-RT405 sy-vline
WA_MTCOR_STR-RT438A sy-vline
WA_MTCOR_STR-RMKOP sy-vline
WA_MTCOR_STR-RMKOL sy-vline
WA_MTCOR_STR-RMLAN sy-vline
WA_MTCOR_STR-RMAPOV sy-vline
WA_MTCOR_STR-RMAWEV sy-vline
WA_MTCOR_STR-RT149 sy-vline
WA_MTCOR_STR-RT134G sy-vline
WA_MTCOR_STR-RV134G sy-vline
WA_MTCOR_STR-RT134H sy-vline
WA_MTCOR_STR-RMA07V sy-vline
WA_MTCOR_STR-RMB61V sy-vline
WA_MTCOR_STR-RMB06V sy-vline
WA_MTCOR_STR-RMSLB sy-vline
WA_MTCOR_STR-RMSKU sy-vline
WA_MTCOR_STR-RMSKB sy-vline
WA_MTCOR_STR-RMSKA sy-vline
WA_MTCOR_STR-RMSPR sy-vline
WA_MTCOR_STR-RMSSE sy-vline
WA_MTCOR_STR-RMSSL sy-vline
WA_MTCOR_STR-RMSSK sy-vline
WA_MTCOR_STR-RMSSA sy-vline
WA_MTCOR_STR-RMSSQ sy-vline
WA_MTCOR_STR-RMSSS sy-vline
WA_MTCOR_STR-RMSCA sy-vline
WA_MTCOR_STR-RMSCP sy-vline
WA_MTCOR_STR-RMSCS sy-vline
WA_MTCOR_STR-RMSCB sy-vline
WA_MTCOR_STR-RMSOA sy-vline
WA_MTCOR_STR-RMSOP sy-vline
WA_MTCOR_STR-RMSOS sy-vline
WA_MTCOR_STR-RMSOB sy-vline
WA_MTCOR_STR-RMKAL sy-vline
WA_MTCOR_STR-RMFHM sy-vline
WA_MTCOR_STR-RMYMS sy-vline
WA_MTCOR_STR-RMPOP sy-vline
WA_MTCOR_STR-RMSFCD sy-vline
WA_MTCOR_STR-SAMAT sy-vline
WA_MTCOR_STR-WVMAT sy-vline
WA_MTCOR_STR-LVOCA sy-vline
WA_MTCOR_STR-LVOCB sy-vline
WA_MTCOR_STR-REBEW sy-vline
WA_MTCOR_STR-RQBEW sy-vline
WA_MTCOR_STR-MPN_LVORM sy-vline
WA_MTCOR_STR-ROBEW sy-vline
WA_MTCOR_STR-RMDMA sy-vline
WA_MTCOR_STR-RMSTB sy-vline
WA_MTCOR_STR-RMSTE sy-vline
WA_MTCOR_STR-RMSTQ sy-vline
WA_MTCOR_STR-RMCSD sy-vline
WA_MTCOR_STR-RMCSS sy-vline
WA_MTCOR_STR-RMSCD sy-vline
WA_MTCOR_STR-RMSFD sy-vline
WA_MTCOR_STR-RMSFS sy-vline
WA_MTCOR_STR-RMSID sy-vline
WA_MTCOR_STR-RMSIS sy-vline
WA_MTCOR_STR-RMSRD sy-vline
WA_MTCOR_STR-RMSRS sy-vline
WA_MTCOR_STR-IND_HEAD sy-vline
WA_MTCOR_STR-IND_INVALID sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.