ABAP Select data from SAP table F0MMIM_MATSTVAL 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 like F0MMIM_MATSTVAL is it the same and what the table looks 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.

You can see what table F0MMIM_MATSTVAL looks like in an S/4 system here and below is a number of ABAP code snippets to demonstrate how to select data from SAP F0MMIM_MATSTVAL 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 F0MMIM_MATSTVAL. See here for more generic Select statement tips.

DATA: IT_F0MMIM_MATSTVAL TYPE STANDARD TABLE OF F0MMIM_MATSTVAL,
      WA_F0MMIM_MATSTVAL TYPE F0MMIM_MATSTVAL,
      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: <F0MMIM_MATSTVAL> TYPE F0MMIM_MATSTVAL.

*Process all fields in table header/work area as string values, full declaration of the F0MMIM_MATSTVAL 
*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_F0MMIM_MATSTVAL.

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM F0MMIM_MATSTVAL
*  INTO TABLE @DATA(IT_F0MMIM_MATSTVAL2).
*--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_F0MMIM_MATSTVAL INDEX 1 INTO DATA(WA_F0MMIM_MATSTVAL2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_F0MMIM_MATSTVAL ASSIGNING <F0MMIM_MATSTVAL>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<F0MMIM_MATSTVAL>-MANDT = 1.
<F0MMIM_MATSTVAL>-MATERIAL = 1.
<F0MMIM_MATSTVAL>-PLANT = 1.
<F0MMIM_MATSTVAL>-STORAGELOCATION = 1.
<F0MMIM_MATSTVAL>-BATCH = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_F0MMIM_MATSTVAL-SUPPLIER, sy-vline,
WA_F0MMIM_MATSTVAL-SDDOCUMENT, sy-vline,
WA_F0MMIM_MATSTVAL-SDDOCUMENTITEM, sy-vline,
WA_F0MMIM_MATSTVAL-WBSELEMENTINTERNALID, sy-vline,
WA_F0MMIM_MATSTVAL-CUSTOMER, sy-vline,
WA_F0MMIM_MATSTVAL-INVENTORYSPECIALSTOCKTYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_F0MMIM_MATSTVAL 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_F0MMIM_MATSTVAL 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_F0MMIM_MATSTVAL INTO WA_F0MMIM_MATSTVAL. *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 MATERIAL CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_F0MMIM_MATSTVAL-MATERIAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_F0MMIM_MATSTVAL-MATERIAL.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ABPSP, internal->external for field WBSELEMENTINTERNALID CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT' EXPORTING input = WA_F0MMIM_MATSTVAL-WBSELEMENTINTERNALID IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_F0MMIM_MATSTVAL-WBSELEMENTINTERNALID.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit CUNIT, internal->external for field MATERIALBASEUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_F0MMIM_MATSTVAL-MATERIALBASEUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_F0MMIM_MATSTVAL-MATERIALBASEUNIT.
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_F0MMIM_MATSTVAL_STR,
MANDT TYPE STRING,
MATERIAL TYPE STRING,
PLANT TYPE STRING,
STORAGELOCATION TYPE STRING,
BATCH TYPE STRING,
SUPPLIER TYPE STRING,
SDDOCUMENT TYPE STRING,
SDDOCUMENTITEM TYPE STRING,
WBSELEMENTINTERNALID TYPE STRING,
CUSTOMER TYPE STRING,
INVENTORYSPECIALSTOCKTYPE TYPE STRING,
COMPANYCODE TYPE STRING,
MATERIALBASEUNIT TYPE STRING,
CURRENCY TYPE STRING,
DISPLAYCURRENCY TYPE STRING,
INVENTORYVALUATIONTYPE TYPE STRING,
WBSELEMENTEXTERNALID TYPE STRING,
WAREHOUSESTORAGEBIN TYPE STRING,
MATERIALNAME TYPE STRING,
MATERIALGROUP TYPE STRING,
MATERIALGROUPNAME TYPE STRING,
PLANTNAME TYPE STRING,
STORAGELOCATIONNAME TYPE STRING,
INVENTORYSPECIALSTOCKTYPENAME TYPE STRING,
CUSTOMERNAME TYPE STRING,
SUPPLIERNAME TYPE STRING,
SHELFLIFEEXPIRATIONDATE TYPE STRING,
MANUFACTUREDATE TYPE STRING,
CURRENTSTOCK TYPE STRING,
QUALITYINSPECTIONSTOCKQUANTITY TYPE STRING,
RETURNSBLOCKEDSTOCKQUANTITY TYPE STRING,
TRANSFERSTOCKSTORAGELOCQTY TYPE STRING,
TRANSFERSTOCKPLANTQUANTITY TYPE STRING,
STOCKINTRANSITQUANTITY TYPE STRING,
BLOCKEDSTOCKQUANTITY TYPE STRING,
RESTRICTEDSTOCKQUANTITY TYPE STRING,
TIEDEMPTIESSTOCKQUANTITY TYPE STRING,
GOODSRECEIPTBLOCKEDSTOCKQTY TYPE STRING,
CURRENTSTOCKVALUE TYPE STRING,
QUALITYINSPECTIONSTOCKVALUE TYPE STRING,
RETURNSBLOCKEDSTOCKVALUE TYPE STRING,
TRANSFERSTOCKSTORAGELOCVALUE TYPE STRING,
TRANSFERSTOCKPLANTVALUE TYPE STRING,
STOCKINTRANSITVALUE TYPE STRING,
BLOCKEDSTOCKVALUE TYPE STRING,
RESTRICTEDSTOCKVALUE TYPE STRING,
TIEDEMPTIESSTOCKVALUE TYPE STRING,
GOODSRECEIPTBLOCKEDSTOCKVALUE TYPE STRING,
CURRENTSTOCKVALUEINDC TYPE STRING,
QUALITYINSPSTOCKVALUEINDC TYPE STRING,
RETURNSBLOCKEDSTOCKVALUEINDC TYPE STRING,
TRANSFERSTOCKSTORLOCVALUEINDC TYPE STRING,
TRANSFERSTOCKPLANTVALUEINDC TYPE STRING,
STOCKINTRANSITVALUEINDC TYPE STRING,
BLOCKEDSTOCKVALUEINDC TYPE STRING,
RESTRICTEDSTOCKVALUEINDC TYPE STRING,
TIEDEMPTIESSTOCKVALUEINDC TYPE STRING,
GRBLOCKEDSTOCKVALUEINDC TYPE STRING,
CURRENTSTOCKCURVAL TYPE STRING,
QUALITYINSPECTIONSTOCKCURVAL TYPE STRING,
RETURNSBLOCKEDSTOCKCURVAL TYPE STRING,
TRANSFERSTOCKSTORAGELOCCURVAL TYPE STRING,
TRANSFERSTOCKPLANTCURVAL TYPE STRING,
STOCKINTRANSITCURVAL TYPE STRING,
BLOCKEDSTOCKCURVAL TYPE STRING,
RESTRICTEDSTOCKCURVAL TYPE STRING,
TIEDEMPTIESSTOCKCURVAL TYPE STRING,
GOODSRECEIPTBLOCKEDSTOCKCURVAL TYPE STRING,
CURRENTSTOCKCURVALINDC TYPE STRING,
QUALITYINSPSTOCKCURVALINDC TYPE STRING,
RETURNSBLOCKEDSTOCKCURVALINDC TYPE STRING,
TRANSFERSTOCKSTORLOCCURVALINDC TYPE STRING,
TRANSFERSTOCKPLANTCURVALINDC TYPE STRING,
STOCKINTRANSITCURVALINDC TYPE STRING,
BLOCKEDSTOCKCURVALINDC TYPE STRING,
RESTRICTEDSTOCKCURVALINDC TYPE STRING,
TIEDEMPTIESSTOCKCURVALINDC TYPE STRING,
GRBLOCKEDSTOCKCURVALINDC TYPE STRING,END OF T_EKKO_STR. DATA: WA_F0MMIM_MATSTVAL_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_F0MMIM_MATSTVAL_STR-MANDT sy-vline
WA_F0MMIM_MATSTVAL_STR-MATERIAL sy-vline
WA_F0MMIM_MATSTVAL_STR-PLANT sy-vline
WA_F0MMIM_MATSTVAL_STR-STORAGELOCATION sy-vline
WA_F0MMIM_MATSTVAL_STR-BATCH sy-vline
WA_F0MMIM_MATSTVAL_STR-SUPPLIER sy-vline
WA_F0MMIM_MATSTVAL_STR-SDDOCUMENT sy-vline
WA_F0MMIM_MATSTVAL_STR-SDDOCUMENTITEM sy-vline
WA_F0MMIM_MATSTVAL_STR-WBSELEMENTINTERNALID sy-vline
WA_F0MMIM_MATSTVAL_STR-CUSTOMER sy-vline
WA_F0MMIM_MATSTVAL_STR-INVENTORYSPECIALSTOCKTYPE sy-vline
WA_F0MMIM_MATSTVAL_STR-COMPANYCODE sy-vline
WA_F0MMIM_MATSTVAL_STR-MATERIALBASEUNIT sy-vline
WA_F0MMIM_MATSTVAL_STR-CURRENCY sy-vline
WA_F0MMIM_MATSTVAL_STR-DISPLAYCURRENCY sy-vline
WA_F0MMIM_MATSTVAL_STR-INVENTORYVALUATIONTYPE sy-vline
WA_F0MMIM_MATSTVAL_STR-WBSELEMENTEXTERNALID sy-vline
WA_F0MMIM_MATSTVAL_STR-WAREHOUSESTORAGEBIN sy-vline
WA_F0MMIM_MATSTVAL_STR-MATERIALNAME sy-vline
WA_F0MMIM_MATSTVAL_STR-MATERIALGROUP sy-vline
WA_F0MMIM_MATSTVAL_STR-MATERIALGROUPNAME sy-vline
WA_F0MMIM_MATSTVAL_STR-PLANTNAME sy-vline
WA_F0MMIM_MATSTVAL_STR-STORAGELOCATIONNAME sy-vline
WA_F0MMIM_MATSTVAL_STR-INVENTORYSPECIALSTOCKTYPENAME sy-vline
WA_F0MMIM_MATSTVAL_STR-CUSTOMERNAME sy-vline
WA_F0MMIM_MATSTVAL_STR-SUPPLIERNAME sy-vline
WA_F0MMIM_MATSTVAL_STR-SHELFLIFEEXPIRATIONDATE sy-vline
WA_F0MMIM_MATSTVAL_STR-MANUFACTUREDATE sy-vline
WA_F0MMIM_MATSTVAL_STR-CURRENTSTOCK sy-vline
WA_F0MMIM_MATSTVAL_STR-QUALITYINSPECTIONSTOCKQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-RETURNSBLOCKEDSTOCKQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKSTORAGELOCQTY sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKPLANTQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-STOCKINTRANSITQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-BLOCKEDSTOCKQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-RESTRICTEDSTOCKQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-TIEDEMPTIESSTOCKQUANTITY sy-vline
WA_F0MMIM_MATSTVAL_STR-GOODSRECEIPTBLOCKEDSTOCKQTY sy-vline
WA_F0MMIM_MATSTVAL_STR-CURRENTSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-QUALITYINSPECTIONSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-RETURNSBLOCKEDSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKSTORAGELOCVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKPLANTVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-STOCKINTRANSITVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-BLOCKEDSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-RESTRICTEDSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-TIEDEMPTIESSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-GOODSRECEIPTBLOCKEDSTOCKVALUE sy-vline
WA_F0MMIM_MATSTVAL_STR-CURRENTSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-QUALITYINSPSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-RETURNSBLOCKEDSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKSTORLOCVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKPLANTVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-STOCKINTRANSITVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-BLOCKEDSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-RESTRICTEDSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-TIEDEMPTIESSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-GRBLOCKEDSTOCKVALUEINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-CURRENTSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-QUALITYINSPECTIONSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-RETURNSBLOCKEDSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKSTORAGELOCCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKPLANTCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-STOCKINTRANSITCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-BLOCKEDSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-RESTRICTEDSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-TIEDEMPTIESSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-GOODSRECEIPTBLOCKEDSTOCKCURVAL sy-vline
WA_F0MMIM_MATSTVAL_STR-CURRENTSTOCKCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-QUALITYINSPSTOCKCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-RETURNSBLOCKEDSTOCKCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKSTORLOCCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-TRANSFERSTOCKPLANTCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-STOCKINTRANSITCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-BLOCKEDSTOCKCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-RESTRICTEDSTOCKCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-TIEDEMPTIESSTOCKCURVALINDC sy-vline
WA_F0MMIM_MATSTVAL_STR-GRBLOCKEDSTOCKCURVALINDC sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.