ABAP Select data from SAP table PPIDOCITEM 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 PPIDOCITEM 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 PPIDOCITEM. 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 PPIDOCITEM 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_PPIDOCITEM TYPE STANDARD TABLE OF PPIDOCITEM,
      WA_PPIDOCITEM TYPE PPIDOCITEM,
      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: <PPIDOCITEM> TYPE PPIDOCITEM.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PPIDOCITEM
*  INTO TABLE @DATA(IT_PPIDOCITEM2).
*--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_PPIDOCITEM INDEX 1 INTO DATA(WA_PPIDOCITEM2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PPIDOCITEM ASSIGNING <PPIDOCITEM>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PPIDOCITEM>-MANDT = 1.
<PPIDOCITEM>-PHYSICALINVENTORYDOCUMENT = 1.
<PPIDOCITEM>-FISCALYEAR = 1.
<PPIDOCITEM>-PHYSICALINVENTORYDOCUMENTITEM = 1.
<PPIDOCITEM>-PHYSINVTRYSITNID = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PPIDOCITEM-MATERIAL, sy-vline,
WA_PPIDOCITEM-PLANT, sy-vline,
WA_PPIDOCITEM-STORAGELOCATION, sy-vline,
WA_PPIDOCITEM-BATCH, sy-vline,
WA_PPIDOCITEM-INVENTORYSPECIALSTOCKTYPE, sy-vline,
WA_PPIDOCITEM-PHYSICALINVENTORYSTOCKTYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PPIDOCITEM 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_PPIDOCITEM 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_PPIDOCITEM INTO WA_PPIDOCITEM. *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 PHYSICALINVENTORYDOCUMENT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_PPIDOCITEM-PHYSICALINVENTORYDOCUMENT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PPIDOCITEM-PHYSICALINVENTORYDOCUMENT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field FISCALYEAR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_PPIDOCITEM-FISCALYEAR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PPIDOCITEM-FISCALYEAR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

*Conversion exit GJAHR, internal->external for field MATERIALDOCUMENTYEAR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_PPIDOCITEM-MATERIALDOCUMENTYEAR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PPIDOCITEM-MATERIALDOCUMENTYEAR.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit MATN1, internal->external for field CROSSPLANTCONFIGURABLEMATERIAL CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_PPIDOCITEM-CROSSPLANTCONFIGURABLEMATERIAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PPIDOCITEM-CROSSPLANTCONFIGURABLEMATERIAL.
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_PPIDOCITEM_STR,
MANDT TYPE STRING,
PHYSICALINVENTORYDOCUMENT TYPE STRING,
FISCALYEAR TYPE STRING,
PHYSICALINVENTORYDOCUMENTITEM TYPE STRING,
PHYSINVTRYSITNID TYPE STRING,
MATERIAL TYPE STRING,
PLANT TYPE STRING,
STORAGELOCATION TYPE STRING,
BATCH TYPE STRING,
INVENTORYSPECIALSTOCKTYPE TYPE STRING,
PHYSICALINVENTORYSTOCKTYPE TYPE STRING,
GOODSMOVEMENTTYPE TYPE STRING,
SALESORDER TYPE STRING,
SALESORDERITEM TYPE STRING,
SCHEDULELINE TYPE STRING,
SUPPLIER TYPE STRING,
CUSTOMER TYPE STRING,
STOCKOWNER TYPE STRING,
LASTCHANGEUSER TYPE STRING,
LASTCHANGEDATE TYPE STRING,
COUNTEDBYUSER TYPE STRING,
PHYSICALINVENTORYLASTCOUNTDATE TYPE STRING,
ADJUSTMENTPOSTINGMADEBYUSER TYPE STRING,
POSTINGDATE TYPE STRING,
PHYSINVENTORYREFERENCENUMBER TYPE STRING,
PHYSICALINVENTORYITEMISCOUNTED TYPE STRING,
PHYSINVTRYDIFFERENCEISPOSTED TYPE STRING,
PHYSINVTRYITEMISRECOUNTED TYPE STRING,
PHYSINVTRYITEMISDELETED TYPE STRING,
ISHANDLEDINALTVUNITOFMSR TYPE STRING,
PHYSICALINVENTORYITEMISZERO TYPE STRING,
BOOKQTYBFRCOUNTINMATLBASEUNIT TYPE STRING,
QUANTITY TYPE STRING,
MATERIALBASEUNIT TYPE STRING,
QUANTITYINUNITOFENTRY TYPE STRING,
UNITOFENTRY TYPE STRING,
DIFFERENCEAMOUNTINCOCODECRCY TYPE STRING,
CURRENCY TYPE STRING,
ENTEREDSLSAMTINCOCODECRCY TYPE STRING,
SLSPRICEAMOUNTINCOCODECRCY TYPE STRING,
PHYSINVTRYCTAMTINCOCODECRCY TYPE STRING,
BOOKQTYAMOUNTINCOCODECRCY TYPE STRING,
DIFFERENCEQUANTITYINBASEUNIT TYPE STRING,
DIFFSLSPRCAMTINCOCODECRCY TYPE STRING,
DIFFCOUNTEDBOOKAMTINCOCODECRY TYPE STRING,
MATERIALDOCUMENT TYPE STRING,
MATERIALDOCUMENTYEAR TYPE STRING,
MATERIALDOCUMENTITEM TYPE STRING,
PHYSINVTRYRECOUNTDOCUMENT TYPE STRING,
CYCLECOUNTTYPE TYPE STRING,
NMBROFPHYSINVTRYPERFISCALYEAR TYPE STRING,
PHYSINVTRYINTVLFORCYCCOUNTING TYPE STRING,
PHYSINVTRYFLOATTIMECYCCOUNTING TYPE STRING,
WBSELEMENTINTERNALID TYPE STRING,
ISVALUEONLYMATERIAL TYPE STRING,
PRODUCTCATEGORY TYPE STRING,
REASONFORPHYSINVTRYDIFFERENCE TYPE STRING,
CROSSPLANTCONFIGURABLEMATERIAL TYPE STRING,
PHYSINVTRYDIFFISDISTRIBUTED TYPE STRING,END OF T_EKKO_STR. DATA: WA_PPIDOCITEM_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_PPIDOCITEM_STR-MANDT sy-vline
WA_PPIDOCITEM_STR-PHYSICALINVENTORYDOCUMENT sy-vline
WA_PPIDOCITEM_STR-FISCALYEAR sy-vline
WA_PPIDOCITEM_STR-PHYSICALINVENTORYDOCUMENTITEM sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYSITNID sy-vline
WA_PPIDOCITEM_STR-MATERIAL sy-vline
WA_PPIDOCITEM_STR-PLANT sy-vline
WA_PPIDOCITEM_STR-STORAGELOCATION sy-vline
WA_PPIDOCITEM_STR-BATCH sy-vline
WA_PPIDOCITEM_STR-INVENTORYSPECIALSTOCKTYPE sy-vline
WA_PPIDOCITEM_STR-PHYSICALINVENTORYSTOCKTYPE sy-vline
WA_PPIDOCITEM_STR-GOODSMOVEMENTTYPE sy-vline
WA_PPIDOCITEM_STR-SALESORDER sy-vline
WA_PPIDOCITEM_STR-SALESORDERITEM sy-vline
WA_PPIDOCITEM_STR-SCHEDULELINE sy-vline
WA_PPIDOCITEM_STR-SUPPLIER sy-vline
WA_PPIDOCITEM_STR-CUSTOMER sy-vline
WA_PPIDOCITEM_STR-STOCKOWNER sy-vline
WA_PPIDOCITEM_STR-LASTCHANGEUSER sy-vline
WA_PPIDOCITEM_STR-LASTCHANGEDATE sy-vline
WA_PPIDOCITEM_STR-COUNTEDBYUSER sy-vline
WA_PPIDOCITEM_STR-PHYSICALINVENTORYLASTCOUNTDATE sy-vline
WA_PPIDOCITEM_STR-ADJUSTMENTPOSTINGMADEBYUSER sy-vline
WA_PPIDOCITEM_STR-POSTINGDATE sy-vline
WA_PPIDOCITEM_STR-PHYSINVENTORYREFERENCENUMBER sy-vline
WA_PPIDOCITEM_STR-PHYSICALINVENTORYITEMISCOUNTED sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYDIFFERENCEISPOSTED sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYITEMISRECOUNTED sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYITEMISDELETED sy-vline
WA_PPIDOCITEM_STR-ISHANDLEDINALTVUNITOFMSR sy-vline
WA_PPIDOCITEM_STR-PHYSICALINVENTORYITEMISZERO sy-vline
WA_PPIDOCITEM_STR-BOOKQTYBFRCOUNTINMATLBASEUNIT sy-vline
WA_PPIDOCITEM_STR-QUANTITY sy-vline
WA_PPIDOCITEM_STR-MATERIALBASEUNIT sy-vline
WA_PPIDOCITEM_STR-QUANTITYINUNITOFENTRY sy-vline
WA_PPIDOCITEM_STR-UNITOFENTRY sy-vline
WA_PPIDOCITEM_STR-DIFFERENCEAMOUNTINCOCODECRCY sy-vline
WA_PPIDOCITEM_STR-CURRENCY sy-vline
WA_PPIDOCITEM_STR-ENTEREDSLSAMTINCOCODECRCY sy-vline
WA_PPIDOCITEM_STR-SLSPRICEAMOUNTINCOCODECRCY sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYCTAMTINCOCODECRCY sy-vline
WA_PPIDOCITEM_STR-BOOKQTYAMOUNTINCOCODECRCY sy-vline
WA_PPIDOCITEM_STR-DIFFERENCEQUANTITYINBASEUNIT sy-vline
WA_PPIDOCITEM_STR-DIFFSLSPRCAMTINCOCODECRCY sy-vline
WA_PPIDOCITEM_STR-DIFFCOUNTEDBOOKAMTINCOCODECRY sy-vline
WA_PPIDOCITEM_STR-MATERIALDOCUMENT sy-vline
WA_PPIDOCITEM_STR-MATERIALDOCUMENTYEAR sy-vline
WA_PPIDOCITEM_STR-MATERIALDOCUMENTITEM sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYRECOUNTDOCUMENT sy-vline
WA_PPIDOCITEM_STR-CYCLECOUNTTYPE sy-vline
WA_PPIDOCITEM_STR-NMBROFPHYSINVTRYPERFISCALYEAR sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYINTVLFORCYCCOUNTING sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYFLOATTIMECYCCOUNTING sy-vline
WA_PPIDOCITEM_STR-WBSELEMENTINTERNALID sy-vline
WA_PPIDOCITEM_STR-ISVALUEONLYMATERIAL sy-vline
WA_PPIDOCITEM_STR-PRODUCTCATEGORY sy-vline
WA_PPIDOCITEM_STR-REASONFORPHYSINVTRYDIFFERENCE sy-vline
WA_PPIDOCITEM_STR-CROSSPLANTCONFIGURABLEMATERIAL sy-vline
WA_PPIDOCITEM_STR-PHYSINVTRYDIFFISDISTRIBUTED sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.