ABAP Select data from SAP table IMAINTORDOP 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 IMAINTORDOP 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 IMAINTORDOP. 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 IMAINTORDOP 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_IMAINTORDOP TYPE STANDARD TABLE OF IMAINTORDOP,
      WA_IMAINTORDOP TYPE IMAINTORDOP,
      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: <IMAINTORDOP> TYPE IMAINTORDOP.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM IMAINTORDOP
*  INTO TABLE @DATA(IT_IMAINTORDOP2).
*--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_IMAINTORDOP INDEX 1 INTO DATA(WA_IMAINTORDOP2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_IMAINTORDOP ASSIGNING <IMAINTORDOP>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<IMAINTORDOP>-MANDT = 1.
<IMAINTORDOP>-MAINTENANCEORDER = 1.
<IMAINTORDOP>-MAINTENANCEORDEROPERATION = 1.
<IMAINTORDOP>-MAINTENANCEORDERSUBOPERATION = 1.
<IMAINTORDOP>-MAINTORDERROUTINGNUMBER = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_IMAINTORDOP-MAINTORDEROPERATIONCOUNTER, sy-vline,
WA_IMAINTORDOP-PLANT, sy-vline,
WA_IMAINTORDOP-OPERATIONWORKCENTERTYPECODE, sy-vline,
WA_IMAINTORDOP-OPERATIONWORKCENTERINTERNALID, sy-vline,
WA_IMAINTORDOP-OPERATIONCONTROLKEY, sy-vline,
WA_IMAINTORDOP-OPERATIONDESCRIPTION, sy-vline.
ENDLOOP. *Add any further fields from structure WA_IMAINTORDOP 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_IMAINTORDOP 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_IMAINTORDOP INTO WA_IMAINTORDOP. *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 ALPHA, internal->external for field MAINTENANCEORDER CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IMAINTORDOP-MAINTENANCEORDER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMAINTORDOP-MAINTENANCEORDER.
WRITE:/ 'New Value:', ld_input.

*Conversion exit NUMCV, internal->external for field MAINTENANCEORDEROPERATION CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_IMAINTORDOP-MAINTENANCEORDEROPERATION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMAINTORDOP-MAINTENANCEORDEROPERATION.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ISOLA, internal->external for field LANGUAGE CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_IMAINTORDOP-LANGUAGE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMAINTORDOP-LANGUAGE.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

*Conversion exit TPLNR, internal->external for field FUNCTIONALLOCATION CALL FUNCTION 'CONVERSION_EXIT_TPLNR_OUTPUT' EXPORTING input = WA_IMAINTORDOP-FUNCTIONALLOCATION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMAINTORDOP-FUNCTIONALLOCATION.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field PURCHASEREQUISITION CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_IMAINTORDOP-PURCHASEREQUISITION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_IMAINTORDOP-PURCHASEREQUISITION.
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_IMAINTORDOP_STR,
MANDT TYPE STRING,
MAINTENANCEORDER TYPE STRING,
MAINTENANCEORDEROPERATION TYPE STRING,
MAINTENANCEORDERSUBOPERATION TYPE STRING,
MAINTORDERROUTINGNUMBER TYPE STRING,
MAINTORDEROPERATIONCOUNTER TYPE STRING,
PLANT TYPE STRING,
OPERATIONWORKCENTERTYPECODE TYPE STRING,
OPERATIONWORKCENTERINTERNALID TYPE STRING,
OPERATIONCONTROLKEY TYPE STRING,
OPERATIONDESCRIPTION TYPE STRING,
OPERATIONSTANDARDTEXTCODE TYPE STRING,
LANGUAGE TYPE STRING,
NUMBEROFTIMETICKETS TYPE STRING,
OPERATIONPURGINFORECDSEARCHTXT TYPE STRING,
OPERATIONSUPPLIER TYPE STRING,
OPEXTERNALPROCESSINGPRICE TYPE STRING,
OPEXTERNALPROCESSINGPRICEUNIT TYPE STRING,
COSTELEMENT TYPE STRING,
OPEXTERNALPROCESSINGCURRENCY TYPE STRING,
OPERATIONPURCHASINGINFORECORD TYPE STRING,
PURCHASINGORGANIZATION TYPE STRING,
PURCHASINGGROUP TYPE STRING,
MATERIALGROUP TYPE STRING,
OPPURCHASEOUTLINEAGREEMENT TYPE STRING,
OPPURCHASEOUTLINEAGREEMENTITEM TYPE STRING,
OPERATIONREQUISITIONERNAME TYPE STRING,
OPERATIONTRACKINGNUMBER TYPE STRING,
NUMBEROFCAPACITIES TYPE STRING,
OPERATIONWORKPERCENT TYPE STRING,
OPERATIONCALCULATIONCONTROL TYPE STRING,
ACTIVITYTYPE TYPE STRING,
OPERATIONSYSTEMCONDITION TYPE STRING,
OPERATIONGOODSRECIPIENTNAME TYPE STRING,
OPERATIONUNLOADINGPOINTNAME TYPE STRING,
OPERATIONPERSONRESPONSIBLE TYPE STRING,
EQUIPMENT TYPE STRING,
FUNCTIONALLOCATION TYPE STRING,
BUSINESSAREA TYPE STRING,
PROFITCENTER TYPE STRING,
COSTINGSHEET TYPE STRING,
TAXJURISDICTION TYPE STRING,
FUNCTIONALAREA TYPE STRING,
ASSEMBLY TYPE STRING,
MAINTOPERATIONEXECSTAGECODE TYPE STRING,
COSTCTRACTIVITYTYPE TYPE STRING,
REQGCOSTCENTERCONTROLLINGAREA TYPE STRING,
CAPACITYREQUIREMENT TYPE STRING,
CAPACITYREQUIREMENTITEM TYPE STRING,
PURCHASEREQUISITION TYPE STRING,
MAINTORDEROPERATIONINTERNALID TYPE STRING,
PURCHASEREQUISITIONITEM TYPE STRING,
MAINTORDERCONFIRMATION TYPE STRING,
OPERATIONMILESTONEUSAGECODE TYPE STRING,
MAINTORDERCONFCNTRVALUE TYPE STRING,
OPERATIONHASLONGTEXT TYPE STRING,
CONTROLLINGAREA TYPE STRING,
MAINTORDOPPROCESSPHASECODE TYPE STRING,
MAINTORDOPPROCESSSUBPHASECODE TYPE STRING,
DFSMAINTORDOPOBJECTNMBR TYPE STRING,END OF T_EKKO_STR. DATA: WA_IMAINTORDOP_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_IMAINTORDOP_STR-MANDT sy-vline
WA_IMAINTORDOP_STR-MAINTENANCEORDER sy-vline
WA_IMAINTORDOP_STR-MAINTENANCEORDEROPERATION sy-vline
WA_IMAINTORDOP_STR-MAINTENANCEORDERSUBOPERATION sy-vline
WA_IMAINTORDOP_STR-MAINTORDERROUTINGNUMBER sy-vline
WA_IMAINTORDOP_STR-MAINTORDEROPERATIONCOUNTER sy-vline
WA_IMAINTORDOP_STR-PLANT sy-vline
WA_IMAINTORDOP_STR-OPERATIONWORKCENTERTYPECODE sy-vline
WA_IMAINTORDOP_STR-OPERATIONWORKCENTERINTERNALID sy-vline
WA_IMAINTORDOP_STR-OPERATIONCONTROLKEY sy-vline
WA_IMAINTORDOP_STR-OPERATIONDESCRIPTION sy-vline
WA_IMAINTORDOP_STR-OPERATIONSTANDARDTEXTCODE sy-vline
WA_IMAINTORDOP_STR-LANGUAGE sy-vline
WA_IMAINTORDOP_STR-NUMBEROFTIMETICKETS sy-vline
WA_IMAINTORDOP_STR-OPERATIONPURGINFORECDSEARCHTXT sy-vline
WA_IMAINTORDOP_STR-OPERATIONSUPPLIER sy-vline
WA_IMAINTORDOP_STR-OPEXTERNALPROCESSINGPRICE sy-vline
WA_IMAINTORDOP_STR-OPEXTERNALPROCESSINGPRICEUNIT sy-vline
WA_IMAINTORDOP_STR-COSTELEMENT sy-vline
WA_IMAINTORDOP_STR-OPEXTERNALPROCESSINGCURRENCY sy-vline
WA_IMAINTORDOP_STR-OPERATIONPURCHASINGINFORECORD sy-vline
WA_IMAINTORDOP_STR-PURCHASINGORGANIZATION sy-vline
WA_IMAINTORDOP_STR-PURCHASINGGROUP sy-vline
WA_IMAINTORDOP_STR-MATERIALGROUP sy-vline
WA_IMAINTORDOP_STR-OPPURCHASEOUTLINEAGREEMENT sy-vline
WA_IMAINTORDOP_STR-OPPURCHASEOUTLINEAGREEMENTITEM sy-vline
WA_IMAINTORDOP_STR-OPERATIONREQUISITIONERNAME sy-vline
WA_IMAINTORDOP_STR-OPERATIONTRACKINGNUMBER sy-vline
WA_IMAINTORDOP_STR-NUMBEROFCAPACITIES sy-vline
WA_IMAINTORDOP_STR-OPERATIONWORKPERCENT sy-vline
WA_IMAINTORDOP_STR-OPERATIONCALCULATIONCONTROL sy-vline
WA_IMAINTORDOP_STR-ACTIVITYTYPE sy-vline
WA_IMAINTORDOP_STR-OPERATIONSYSTEMCONDITION sy-vline
WA_IMAINTORDOP_STR-OPERATIONGOODSRECIPIENTNAME sy-vline
WA_IMAINTORDOP_STR-OPERATIONUNLOADINGPOINTNAME sy-vline
WA_IMAINTORDOP_STR-OPERATIONPERSONRESPONSIBLE sy-vline
WA_IMAINTORDOP_STR-EQUIPMENT sy-vline
WA_IMAINTORDOP_STR-FUNCTIONALLOCATION sy-vline
WA_IMAINTORDOP_STR-BUSINESSAREA sy-vline
WA_IMAINTORDOP_STR-PROFITCENTER sy-vline
WA_IMAINTORDOP_STR-COSTINGSHEET sy-vline
WA_IMAINTORDOP_STR-TAXJURISDICTION sy-vline
WA_IMAINTORDOP_STR-FUNCTIONALAREA sy-vline
WA_IMAINTORDOP_STR-ASSEMBLY sy-vline
WA_IMAINTORDOP_STR-MAINTOPERATIONEXECSTAGECODE sy-vline
WA_IMAINTORDOP_STR-COSTCTRACTIVITYTYPE sy-vline
WA_IMAINTORDOP_STR-REQGCOSTCENTERCONTROLLINGAREA sy-vline
WA_IMAINTORDOP_STR-CAPACITYREQUIREMENT sy-vline
WA_IMAINTORDOP_STR-CAPACITYREQUIREMENTITEM sy-vline
WA_IMAINTORDOP_STR-PURCHASEREQUISITION sy-vline
WA_IMAINTORDOP_STR-MAINTORDEROPERATIONINTERNALID sy-vline
WA_IMAINTORDOP_STR-PURCHASEREQUISITIONITEM sy-vline
WA_IMAINTORDOP_STR-MAINTORDERCONFIRMATION sy-vline
WA_IMAINTORDOP_STR-OPERATIONMILESTONEUSAGECODE sy-vline
WA_IMAINTORDOP_STR-MAINTORDERCONFCNTRVALUE sy-vline
WA_IMAINTORDOP_STR-OPERATIONHASLONGTEXT sy-vline
WA_IMAINTORDOP_STR-CONTROLLINGAREA sy-vline
WA_IMAINTORDOP_STR-MAINTORDOPPROCESSPHASECODE sy-vline
WA_IMAINTORDOP_STR-MAINTORDOPPROCESSSUBPHASECODE sy-vline
WA_IMAINTORDOP_STR-DFSMAINTORDOPOBJECTNMBR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.