ABAP Select data from SAP table M61DM into internal table



Get Example source ABAP code based on a different SAP table
  

So we all know S/4Hana is the latest version and the future of SAP but what about the selecting data from hana tables, is it 5he same and what do tables like M61DM look like in an S/4 system. If you havent seen or logged into an S/4Hana system you might be surprised at how similar it looks to the previous ERP/ECC version. Infact when using the gui it is difficult to tell the difference, and selecting data from database tables the same is true. Infact even for tables that have been replaced in hana, SAP have purposfully created versions of the old/original tables for backward compatability. This means your existing ABAP code will still work while you implement changes that take advantage of the new functionality and Hana in-memory tables.

Below is a number of ABAP code snippets to demonstrate how to select data from SAP M61DM 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 M61DM. See here for more generic Select statement tips.

DATA: IT_M61DM TYPE STANDARD TABLE OF M61DM,
      WA_M61DM TYPE M61DM,
      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: <M61DM> TYPE M61DM.

*Process all fields in table header/work area as string values, full declaration of the M61DM 
*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.
  PERFORM process_as_string_field_values CHANGING wa_M61DM.

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM M61DM
*  INTO TABLE @DATA(IT_M61DM2).
*--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_M61DM INDEX 1 INTO DATA(WA_M61DM2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_M61DM ASSIGNING <M61DM>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<M61DM>-MATNR = 1.
<M61DM>-WERKS = 1.
<M61DM>-LVORM = 1.
<M61DM>-MAKTX = 1.
<M61DM>-MTART = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_M61DM-MATKL, sy-vline,
WA_M61DM-MEINS, sy-vline,
WA_M61DM-MMSTA, sy-vline,
WA_M61DM-DISMM, sy-vline,
WA_M61DM-BESKZ, sy-vline,
WA_M61DM-SOBSL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_M61DM 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_M61DM 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_M61DM INTO WA_M61DM. *Write horizonal line to screen report. WRITE:/ sy-uline. *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. This form converts 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_M61DM-MATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-MATNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field MEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_M61DM-MEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-MEINS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATN1, internal->external for field NFMAT CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_M61DM-NFMAT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-NFMAT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATN1, internal->external for field PRGRP CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_M61DM-PRGRP IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-PRGRP.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field PRBME CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_M61DM-PRBME IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-PRBME.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATN1, internal->external for field STDPD CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_M61DM-STDPD IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-STDPD.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field BSTME CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_M61DM-BSTME IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-BSTME.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field AUSME CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_M61DM-AUSME IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-AUSME.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field EXPME CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_M61DM-EXPME IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-EXPME.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field FRTME CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_M61DM-FRTME IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-FRTME.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATN1, internal->external for field SATNR CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_M61DM-SATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-SATNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field MFRNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_M61DM-MFRNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-MFRNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PRCTR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_M61DM-PRCTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-PRCTR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit MATN1, internal->external for field IMATN CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_M61DM-IMATN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-IMATN.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PICNUM CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_M61DM-PICNUM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_M61DM-PICNUM.
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_M61DM_STR,
MATNR TYPE STRING,
WERKS TYPE STRING,
LVORM TYPE STRING,
MAKTX TYPE STRING,
MTART TYPE STRING,
MATKL TYPE STRING,
MEINS TYPE STRING,
MMSTA TYPE STRING,
DISMM TYPE STRING,
BESKZ TYPE STRING,
SOBSL TYPE STRING,
MISKZ TYPE STRING,
DISST TYPE STRING,
SBDKZ TYPE STRING,
KZBED TYPE STRING,
DISPO TYPE STRING,
EKGRP TYPE STRING,
KZDIE TYPE STRING,
DZEIT TYPE STRING,
BEARZ TYPE STRING,
RUEZT TYPE STRING,
TRANZ TYPE STRING,
BASMG TYPE STRING,
FHORI TYPE STRING,
PLIFZ TYPE STRING,
WEBAZ TYPE STRING,
WZEIT TYPE STRING,
PFREI TYPE STRING,
DISLS TYPE STRING,
BSTFE TYPE STRING,
BSTMI TYPE STRING,
BSTMA TYPE STRING,
BSTRF TYPE STRING,
MABST TYPE STRING,
AUSSS TYPE STRING,
EISBE TYPE STRING,
MINBE TYPE STRING,
LOSFX TYPE STRING,
LAGPR TYPE STRING,
PROKZ TYPE STRING,
PERKZ TYPE STRING,
AUFTL TYPE STRING,
KZAUS TYPE STRING,
AUSDT TYPE STRING,
NFMAT TYPE STRING,
ALTSL TYPE STRING,
BKLAS TYPE STRING,
VPRSV TYPE STRING,
VERPR TYPE STRING,
STPRS TYPE STRING,
PEINH TYPE STRING,
USEQU TYPE STRING,
PERIV TYPE STRING,
MTVFP TYPE STRING,
WERTU TYPE STRING,
MENGU TYPE STRING,
SAUFT TYPE STRING,
MRPPP TYPE STRING,
LFRHY TYPE STRING,
DISGR TYPE STRING,
PSTAT TYPE STRING,
VRMOD TYPE STRING,
VINT1 TYPE STRING,
VINT2 TYPE STRING,
FXHOR TYPE STRING,
QMATV TYPE STRING,
PRGRP TYPE STRING,
PRWRK TYPE STRING,
UMREF TYPE STRING,
PRBME TYPE STRING,
TAKZT TYPE STRING,
FEVOR TYPE STRING,
RWPRO TYPE STRING,
KZREV TYPE STRING,
KZKFG TYPE STRING,
STDPD TYPE STRING,
CUOBJ TYPE STRING,
CUOBF TYPE STRING,
LGPRO TYPE STRING,
SFEPR TYPE STRING,
RDPRF TYPE STRING,
STRGR TYPE STRING,
LGFSB TYPE STRING,
BSTME TYPE STRING,
AUSME TYPE STRING,
EXPME TYPE STRING,
FRTME TYPE STRING,
SERNP TYPE STRING,
EPRIO TYPE STRING,
CUOBV TYPE STRING,
BSEXT TYPE STRING,
BSINT TYPE STRING,
KZGRP TYPE STRING,
INSMK TYPE STRING,
RESVP TYPE STRING,
XCHPF TYPE STRING,
SHFLG TYPE STRING,
SHZET TYPE STRING,
MDACH TYPE STRING,
BWSCL TYPE STRING,
SPART TYPE STRING,
ATTYP TYPE STRING,
SATNR TYPE STRING,
KZWSM TYPE STRING,
FABKZ TYPE STRING,
SERLV TYPE STRING,
VSPVB TYPE STRING,
DPLFS TYPE STRING,
DPLPU TYPE STRING,
DPLHO TYPE STRING,
MMSTD TYPE STRING,
MSTAE TYPE STRING,
MSTDE TYPE STRING,
MFRPN TYPE STRING,
MFRNR TYPE STRING,
MPROF TYPE STRING,
PRCTR TYPE STRING,
KZEFF TYPE STRING,
MAABC TYPE STRING,
SHPRO TYPE STRING,
DIBER TYPE STRING,
AHDIS TYPE STRING,
APOKZ TYPE STRING,
SCHGT TYPE STRING,
BERID TYPE STRING,
KZPSP TYPE STRING,
EISLO TYPE STRING,
BFLME TYPE STRING,
SGT_REL TYPE STRING,
SGT_COVS TYPE STRING,
SGT_STATC TYPE STRING,
SGT_PRCM TYPE STRING,
SGT_SCOPE TYPE STRING,
SGT_STK_PRT TYPE STRING,
SGT_MRPSI TYPE STRING,
PPSKZ TYPE STRING,
FSH_MG_ARUN_REQ TYPE STRING,
FPRFM TYPE STRING,
IMATN TYPE STRING,
PICNUM TYPE STRING,END OF T_EKKO_STR. DATA: WA_M61DM_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_M61DM_STR-MATNR sy-vline
WA_M61DM_STR-WERKS sy-vline
WA_M61DM_STR-LVORM sy-vline
WA_M61DM_STR-MAKTX sy-vline
WA_M61DM_STR-MTART sy-vline
WA_M61DM_STR-MATKL sy-vline
WA_M61DM_STR-MEINS sy-vline
WA_M61DM_STR-MMSTA sy-vline
WA_M61DM_STR-DISMM sy-vline
WA_M61DM_STR-BESKZ sy-vline
WA_M61DM_STR-SOBSL sy-vline
WA_M61DM_STR-MISKZ sy-vline
WA_M61DM_STR-DISST sy-vline
WA_M61DM_STR-SBDKZ sy-vline
WA_M61DM_STR-KZBED sy-vline
WA_M61DM_STR-DISPO sy-vline
WA_M61DM_STR-EKGRP sy-vline
WA_M61DM_STR-KZDIE sy-vline
WA_M61DM_STR-DZEIT sy-vline
WA_M61DM_STR-BEARZ sy-vline
WA_M61DM_STR-RUEZT sy-vline
WA_M61DM_STR-TRANZ sy-vline
WA_M61DM_STR-BASMG sy-vline
WA_M61DM_STR-FHORI sy-vline
WA_M61DM_STR-PLIFZ sy-vline
WA_M61DM_STR-WEBAZ sy-vline
WA_M61DM_STR-WZEIT sy-vline
WA_M61DM_STR-PFREI sy-vline
WA_M61DM_STR-DISLS sy-vline
WA_M61DM_STR-BSTFE sy-vline
WA_M61DM_STR-BSTMI sy-vline
WA_M61DM_STR-BSTMA sy-vline
WA_M61DM_STR-BSTRF sy-vline
WA_M61DM_STR-MABST sy-vline
WA_M61DM_STR-AUSSS sy-vline
WA_M61DM_STR-EISBE sy-vline
WA_M61DM_STR-MINBE sy-vline
WA_M61DM_STR-LOSFX sy-vline
WA_M61DM_STR-LAGPR sy-vline
WA_M61DM_STR-PROKZ sy-vline
WA_M61DM_STR-PERKZ sy-vline
WA_M61DM_STR-AUFTL sy-vline
WA_M61DM_STR-KZAUS sy-vline
WA_M61DM_STR-AUSDT sy-vline
WA_M61DM_STR-NFMAT sy-vline
WA_M61DM_STR-ALTSL sy-vline
WA_M61DM_STR-BKLAS sy-vline
WA_M61DM_STR-VPRSV sy-vline
WA_M61DM_STR-VERPR sy-vline
WA_M61DM_STR-STPRS sy-vline
WA_M61DM_STR-PEINH sy-vline
WA_M61DM_STR-USEQU sy-vline
WA_M61DM_STR-PERIV sy-vline
WA_M61DM_STR-MTVFP sy-vline
WA_M61DM_STR-WERTU sy-vline
WA_M61DM_STR-MENGU sy-vline
WA_M61DM_STR-SAUFT sy-vline
WA_M61DM_STR-MRPPP sy-vline
WA_M61DM_STR-LFRHY sy-vline
WA_M61DM_STR-DISGR sy-vline
WA_M61DM_STR-PSTAT sy-vline
WA_M61DM_STR-VRMOD sy-vline
WA_M61DM_STR-VINT1 sy-vline
WA_M61DM_STR-VINT2 sy-vline
WA_M61DM_STR-FXHOR sy-vline
WA_M61DM_STR-QMATV sy-vline
WA_M61DM_STR-PRGRP sy-vline
WA_M61DM_STR-PRWRK sy-vline
WA_M61DM_STR-UMREF sy-vline
WA_M61DM_STR-PRBME sy-vline
WA_M61DM_STR-TAKZT sy-vline
WA_M61DM_STR-FEVOR sy-vline
WA_M61DM_STR-RWPRO sy-vline
WA_M61DM_STR-KZREV sy-vline
WA_M61DM_STR-KZKFG sy-vline
WA_M61DM_STR-STDPD sy-vline
WA_M61DM_STR-CUOBJ sy-vline
WA_M61DM_STR-CUOBF sy-vline
WA_M61DM_STR-LGPRO sy-vline
WA_M61DM_STR-SFEPR sy-vline
WA_M61DM_STR-RDPRF sy-vline
WA_M61DM_STR-STRGR sy-vline
WA_M61DM_STR-LGFSB sy-vline
WA_M61DM_STR-BSTME sy-vline
WA_M61DM_STR-AUSME sy-vline
WA_M61DM_STR-EXPME sy-vline
WA_M61DM_STR-FRTME sy-vline
WA_M61DM_STR-SERNP sy-vline
WA_M61DM_STR-EPRIO sy-vline
WA_M61DM_STR-CUOBV sy-vline
WA_M61DM_STR-BSEXT sy-vline
WA_M61DM_STR-BSINT sy-vline
WA_M61DM_STR-KZGRP sy-vline
WA_M61DM_STR-INSMK sy-vline
WA_M61DM_STR-RESVP sy-vline
WA_M61DM_STR-XCHPF sy-vline
WA_M61DM_STR-SHFLG sy-vline
WA_M61DM_STR-SHZET sy-vline
WA_M61DM_STR-MDACH sy-vline
WA_M61DM_STR-BWSCL sy-vline
WA_M61DM_STR-SPART sy-vline
WA_M61DM_STR-ATTYP sy-vline
WA_M61DM_STR-SATNR sy-vline
WA_M61DM_STR-KZWSM sy-vline
WA_M61DM_STR-FABKZ sy-vline
WA_M61DM_STR-SERLV sy-vline
WA_M61DM_STR-VSPVB sy-vline
WA_M61DM_STR-DPLFS sy-vline
WA_M61DM_STR-DPLPU sy-vline
WA_M61DM_STR-DPLHO sy-vline
WA_M61DM_STR-MMSTD sy-vline
WA_M61DM_STR-MSTAE sy-vline
WA_M61DM_STR-MSTDE sy-vline
WA_M61DM_STR-MFRPN sy-vline
WA_M61DM_STR-MFRNR sy-vline
WA_M61DM_STR-MPROF sy-vline
WA_M61DM_STR-PRCTR sy-vline
WA_M61DM_STR-KZEFF sy-vline
WA_M61DM_STR-MAABC sy-vline
WA_M61DM_STR-SHPRO sy-vline
WA_M61DM_STR-DIBER sy-vline
WA_M61DM_STR-AHDIS sy-vline
WA_M61DM_STR-APOKZ sy-vline
WA_M61DM_STR-SCHGT sy-vline
WA_M61DM_STR-BERID sy-vline
WA_M61DM_STR-KZPSP sy-vline
WA_M61DM_STR-EISLO sy-vline
WA_M61DM_STR-BFLME sy-vline
WA_M61DM_STR-SGT_REL sy-vline
WA_M61DM_STR-SGT_COVS sy-vline
WA_M61DM_STR-SGT_STATC sy-vline
WA_M61DM_STR-SGT_PRCM sy-vline
WA_M61DM_STR-SGT_SCOPE sy-vline
WA_M61DM_STR-SGT_STK_PRT sy-vline
WA_M61DM_STR-SGT_MRPSI sy-vline
WA_M61DM_STR-PPSKZ sy-vline
WA_M61DM_STR-FSH_MG_ARUN_REQ sy-vline
WA_M61DM_STR-FPRFM sy-vline
WA_M61DM_STR-IMATN sy-vline
WA_M61DM_STR-PICNUM sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.