ABAP Select data from SAP table IMATSTLOC 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 IMATSTLOC 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 IMATSTLOC. 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 IMATSTLOC 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_IMATSTLOC TYPE STANDARD TABLE OF IMATSTLOC, WA_IMATSTLOC TYPE IMATSTLOC, 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: <IMATSTLOC> TYPE IMATSTLOC. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_IMATSTLOC. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM IMATSTLOC INTO TABLE IT_IMATSTLOC. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM IMATSTLOC * INTO TABLE @DATA(IT_IMATSTLOC2). *--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_IMATSTLOC INDEX 1 INTO DATA(WA_IMATSTLOC2). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_IMATSTLOC ASSIGNING <IMATSTLOC>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<IMATSTLOC>-MANDT = 1.
<IMATSTLOC>-MATERIAL = 1.
<IMATSTLOC>-PLANT = 1.
<IMATSTLOC>-STORAGELOCATION = 1.
<IMATSTLOC>-ABCINDICATOR = 1.
ENDLOOP. LOOP AT IT_IMATSTLOC INTO WA_IMATSTLOC. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_IMATSTLOC-AVAILABILITYCHECKTYPE, sy-vline,
WA_IMATSTLOC-COMMODITY, sy-vline,
WA_IMATSTLOC-CONSIGNMENTCONTROL, sy-vline,
WA_IMATSTLOC-CONSUMPTIONQTYMULTIPLIER, sy-vline,
WA_IMATSTLOC-CONSUMPTIONREFUSAGEENDDATE, sy-vline,
WA_IMATSTLOC-CONSUMPTIONREFERENCEPLANT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_IMATSTLOC 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_IMATSTLOC 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_IMATSTLOC INTO WA_IMATSTLOC. *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 MATN1, internal->external for field MATERIAL CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_IMATSTLOC-MATERIAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-MATERIAL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit MATN1, internal->external for field CONSUMPTIONREFERENCEPRODUCT CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_IMATSTLOC-CONSUMPTIONREFERENCEPRODUCT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-CONSUMPTIONREFERENCEPRODUCT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit GJAHR, internal->external for field FISCALYEARCURRENTINVTRYPERIOD CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_IMATSTLOC-FISCALYEARCURRENTINVTRYPERIOD IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-FISCALYEARCURRENTINVTRYPERIOD.
WRITE:/ 'New Value:', ld_input.
*Conversion exit CUNIT, internal->external for field GOODSISSUEUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_IMATSTLOC-GOODSISSUEUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-GOODSISSUEUNIT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit CUNIT, internal->external for field MATERIALBASEUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_IMATSTLOC-MATERIALBASEUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-MATERIALBASEUNIT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field MATERIALMANUFACTURERNUMBER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IMATSTLOC-MATERIALMANUFACTURERNUMBER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-MATERIALMANUFACTURERNUMBER.
WRITE:/ 'New Value:', ld_input.
*Conversion exit CUNIT, internal->external for field MATERIALWEIGHTUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_IMATSTLOC-MATERIALWEIGHTUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-MATERIALWEIGHTUNIT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit MATN1, internal->external for field PRODUCT CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_IMATSTLOC-PRODUCT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-PRODUCT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field PRODUCTCFOPCATEGORY CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IMATSTLOC-PRODUCTCFOPCATEGORY IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-PRODUCTCFOPCATEGORY.
WRITE:/ 'New Value:', ld_input.
*Conversion exit MATN1, internal->external for field PRODUCTISCONFIGURABLE CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_IMATSTLOC-PRODUCTISCONFIGURABLE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-PRODUCTISCONFIGURABLE.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field PROFITCENTER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IMATSTLOC-PROFITCENTER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMATSTLOC-PROFITCENTER.
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_IMATSTLOC_STR,
MANDT TYPE STRING,
MATERIAL TYPE STRING,
PLANT TYPE STRING,
STORAGELOCATION TYPE STRING,
ABCINDICATOR TYPE STRING,
AVAILABILITYCHECKTYPE TYPE STRING,
COMMODITY TYPE STRING,
CONSIGNMENTCONTROL TYPE STRING,
CONSUMPTIONQTYMULTIPLIER TYPE STRING,
CONSUMPTIONREFUSAGEENDDATE TYPE STRING,
CONSUMPTIONREFERENCEPLANT TYPE STRING,
CONSUMPTIONREFERENCEPRODUCT TYPE STRING,
CONSUMPTIONTAXCTRLCODE TYPE STRING,
COUNTRYOFORIGIN TYPE STRING,
CREATIONDATE TYPE STRING,
DATEOFLASTPOSTEDCNTUNRSTRCDSTK TYPE STRING,
DISTRCNTRDISTRIBUTIONPROFILE TYPE STRING,
FISCALMONTHCURRENTPERIOD TYPE STRING,
FISCALYEARCURRENTINVTRYPERIOD TYPE STRING,
FISCALYEARCURRENTPERIOD TYPE STRING,
FISCALYEARVARIANT TYPE STRING,
FIXEDLOTSIZEQUANTITY TYPE STRING,
GOODISSUEPROCESSINGDAYS TYPE STRING,
GOODSISSUEUNIT TYPE STRING,
GOODSRECEIPTBLOCKEDSTOCKQTY TYPE STRING,
GOODSRECEIPTDURATION TYPE STRING,
HASCONSIGNMENTCTRL TYPE STRING,
HASINVTRYBLOCKSTOCKPREVPERIOD TYPE STRING,
HASPOSTTOINSPECTIONSTOCK TYPE STRING,
INVENTORYBLOCKSTOCKIND TYPE STRING,
INVENTORYCORRECTIONFACTOR TYPE STRING,
INVENTORYSTOCKPREVPERIOD TYPE STRING,
INVTRYCURRENTYEARSTOCKIND TYPE STRING,
INVTRYQUALINSPCURRENTYRSTKIND TYPE STRING,
INVTRYRESTSTOCKPREVPERIODIND TYPE STRING,
INVTRYRESTRICTEDUSESTOCKIND TYPE STRING,
INVTRYSTOCKQLTYINSPPREVPERIOD TYPE STRING,
ISBATCHMANAGEMENTREQUIRED TYPE STRING,
ISCOPRODUCT TYPE STRING,
ISINTERNALBATCHMANAGED TYPE STRING,
ISNEGATIVESTOCKALLOWED TYPE STRING,
ISPURGACROSSPURGGROUP TYPE STRING,
MRPRESPONSIBLE TYPE STRING,
MRPTYPE TYPE STRING,
MAINTENANCESTATUS TYPE STRING,
MAINTENANCESTATUSNAME TYPE STRING,
MATERIALBASEUNIT TYPE STRING,
MATERIALGROSSWEIGHT TYPE STRING,
MATERIALGROUP TYPE STRING,
MATERIALMANUFACTURERNUMBER TYPE STRING,
MATERIALMANUFACTURERPARTNUMBER TYPE STRING,
MATERIALNETWEIGHT TYPE STRING,
MATERIALTYPE TYPE STRING,
MATERIALWEIGHTUNIT TYPE STRING,
MAXIMUMLOTSIZEQUANTITY TYPE STRING,
MINIMUMLOTSIZEQUANTITY TYPE STRING,
OVERDELIVTOLERANCELIMIT TYPE STRING,
PERIODTYPE TYPE STRING,
PHYSICALINVENTORYBLOCKIND TYPE STRING,
PROCUREMENTTYPE TYPE STRING,
PRODUCT TYPE STRING,
PRODUCTCFOPCATEGORY TYPE STRING,
PRODUCTISCONFIGURABLE TYPE STRING,
PRODUCTUNITGROUP TYPE STRING,
PRODUCTIONINVTRYMANAGEDLOC TYPE STRING,
PRODUCTIONSCHEDULINGPROFILE TYPE STRING,
PRODUCTIONSUPERVISOR TYPE STRING,
PROFILECODE TYPE STRING,
PROFILEVALIDITYSTARTDATE TYPE STRING,
PROFITCENTER TYPE STRING,
PURCHASINGGROUP TYPE STRING,
REGIONOFORIGIN TYPE STRING,
SAFETYSTOCKQUANTITY TYPE STRING,
SERIALNUMBERPROFILE TYPE STRING,
SOURCEOFSUPPLYCATEGORY TYPE STRING,
SPECIALPROCUREMENTTYPE TYPE STRING,
STOCKDETERMINATIONGROUP TYPE STRING,
STOCKINTRANSFERQUANTITY TYPE STRING,
STOCKINTRANSITQUANTITY TYPE STRING,
UNDERDELIVTOLERANCELIMIT TYPE STRING,
WAREHOUSESTORAGEBIN TYPE STRING,
AUTHORIZATIONGROUP TYPE STRING,END OF T_EKKO_STR. DATA: WA_IMATSTLOC_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_IMATSTLOC_STR-MANDT sy-vline
WA_IMATSTLOC_STR-MATERIAL sy-vline
WA_IMATSTLOC_STR-PLANT sy-vline
WA_IMATSTLOC_STR-STORAGELOCATION sy-vline
WA_IMATSTLOC_STR-ABCINDICATOR sy-vline
WA_IMATSTLOC_STR-AVAILABILITYCHECKTYPE sy-vline
WA_IMATSTLOC_STR-COMMODITY sy-vline
WA_IMATSTLOC_STR-CONSIGNMENTCONTROL sy-vline
WA_IMATSTLOC_STR-CONSUMPTIONQTYMULTIPLIER sy-vline
WA_IMATSTLOC_STR-CONSUMPTIONREFUSAGEENDDATE sy-vline
WA_IMATSTLOC_STR-CONSUMPTIONREFERENCEPLANT sy-vline
WA_IMATSTLOC_STR-CONSUMPTIONREFERENCEPRODUCT sy-vline
WA_IMATSTLOC_STR-CONSUMPTIONTAXCTRLCODE sy-vline
WA_IMATSTLOC_STR-COUNTRYOFORIGIN sy-vline
WA_IMATSTLOC_STR-CREATIONDATE sy-vline
WA_IMATSTLOC_STR-DATEOFLASTPOSTEDCNTUNRSTRCDSTK sy-vline
WA_IMATSTLOC_STR-DISTRCNTRDISTRIBUTIONPROFILE sy-vline
WA_IMATSTLOC_STR-FISCALMONTHCURRENTPERIOD sy-vline
WA_IMATSTLOC_STR-FISCALYEARCURRENTINVTRYPERIOD sy-vline
WA_IMATSTLOC_STR-FISCALYEARCURRENTPERIOD sy-vline
WA_IMATSTLOC_STR-FISCALYEARVARIANT sy-vline
WA_IMATSTLOC_STR-FIXEDLOTSIZEQUANTITY sy-vline
WA_IMATSTLOC_STR-GOODISSUEPROCESSINGDAYS sy-vline
WA_IMATSTLOC_STR-GOODSISSUEUNIT sy-vline
WA_IMATSTLOC_STR-GOODSRECEIPTBLOCKEDSTOCKQTY sy-vline
WA_IMATSTLOC_STR-GOODSRECEIPTDURATION sy-vline
WA_IMATSTLOC_STR-HASCONSIGNMENTCTRL sy-vline
WA_IMATSTLOC_STR-HASINVTRYBLOCKSTOCKPREVPERIOD sy-vline
WA_IMATSTLOC_STR-HASPOSTTOINSPECTIONSTOCK sy-vline
WA_IMATSTLOC_STR-INVENTORYBLOCKSTOCKIND sy-vline
WA_IMATSTLOC_STR-INVENTORYCORRECTIONFACTOR sy-vline
WA_IMATSTLOC_STR-INVENTORYSTOCKPREVPERIOD sy-vline
WA_IMATSTLOC_STR-INVTRYCURRENTYEARSTOCKIND sy-vline
WA_IMATSTLOC_STR-INVTRYQUALINSPCURRENTYRSTKIND sy-vline
WA_IMATSTLOC_STR-INVTRYRESTSTOCKPREVPERIODIND sy-vline
WA_IMATSTLOC_STR-INVTRYRESTRICTEDUSESTOCKIND sy-vline
WA_IMATSTLOC_STR-INVTRYSTOCKQLTYINSPPREVPERIOD sy-vline
WA_IMATSTLOC_STR-ISBATCHMANAGEMENTREQUIRED sy-vline
WA_IMATSTLOC_STR-ISCOPRODUCT sy-vline
WA_IMATSTLOC_STR-ISINTERNALBATCHMANAGED sy-vline
WA_IMATSTLOC_STR-ISNEGATIVESTOCKALLOWED sy-vline
WA_IMATSTLOC_STR-ISPURGACROSSPURGGROUP sy-vline
WA_IMATSTLOC_STR-MRPRESPONSIBLE sy-vline
WA_IMATSTLOC_STR-MRPTYPE sy-vline
WA_IMATSTLOC_STR-MAINTENANCESTATUS sy-vline
WA_IMATSTLOC_STR-MAINTENANCESTATUSNAME sy-vline
WA_IMATSTLOC_STR-MATERIALBASEUNIT sy-vline
WA_IMATSTLOC_STR-MATERIALGROSSWEIGHT sy-vline
WA_IMATSTLOC_STR-MATERIALGROUP sy-vline
WA_IMATSTLOC_STR-MATERIALMANUFACTURERNUMBER sy-vline
WA_IMATSTLOC_STR-MATERIALMANUFACTURERPARTNUMBER sy-vline
WA_IMATSTLOC_STR-MATERIALNETWEIGHT sy-vline
WA_IMATSTLOC_STR-MATERIALTYPE sy-vline
WA_IMATSTLOC_STR-MATERIALWEIGHTUNIT sy-vline
WA_IMATSTLOC_STR-MAXIMUMLOTSIZEQUANTITY sy-vline
WA_IMATSTLOC_STR-MINIMUMLOTSIZEQUANTITY sy-vline
WA_IMATSTLOC_STR-OVERDELIVTOLERANCELIMIT sy-vline
WA_IMATSTLOC_STR-PERIODTYPE sy-vline
WA_IMATSTLOC_STR-PHYSICALINVENTORYBLOCKIND sy-vline
WA_IMATSTLOC_STR-PROCUREMENTTYPE sy-vline
WA_IMATSTLOC_STR-PRODUCT sy-vline
WA_IMATSTLOC_STR-PRODUCTCFOPCATEGORY sy-vline
WA_IMATSTLOC_STR-PRODUCTISCONFIGURABLE sy-vline
WA_IMATSTLOC_STR-PRODUCTUNITGROUP sy-vline
WA_IMATSTLOC_STR-PRODUCTIONINVTRYMANAGEDLOC sy-vline
WA_IMATSTLOC_STR-PRODUCTIONSCHEDULINGPROFILE sy-vline
WA_IMATSTLOC_STR-PRODUCTIONSUPERVISOR sy-vline
WA_IMATSTLOC_STR-PROFILECODE sy-vline
WA_IMATSTLOC_STR-PROFILEVALIDITYSTARTDATE sy-vline
WA_IMATSTLOC_STR-PROFITCENTER sy-vline
WA_IMATSTLOC_STR-PURCHASINGGROUP sy-vline
WA_IMATSTLOC_STR-REGIONOFORIGIN sy-vline
WA_IMATSTLOC_STR-SAFETYSTOCKQUANTITY sy-vline
WA_IMATSTLOC_STR-SERIALNUMBERPROFILE sy-vline
WA_IMATSTLOC_STR-SOURCEOFSUPPLYCATEGORY sy-vline
WA_IMATSTLOC_STR-SPECIALPROCUREMENTTYPE sy-vline
WA_IMATSTLOC_STR-STOCKDETERMINATIONGROUP sy-vline
WA_IMATSTLOC_STR-STOCKINTRANSFERQUANTITY sy-vline
WA_IMATSTLOC_STR-STOCKINTRANSITQUANTITY sy-vline
WA_IMATSTLOC_STR-UNDERDELIVTOLERANCELIMIT sy-vline
WA_IMATSTLOC_STR-WAREHOUSESTORAGEBIN sy-vline
WA_IMATSTLOC_STR-AUTHORIZATIONGROUP sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.