ABAP Select data from SAP table WSTI_WD_INV_DOC_ITEM 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 WSTI_WD_INV_DOC_ITEM 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 WSTI_WD_INV_DOC_ITEM. 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 WSTI_WD_INV_DOC_ITEM 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_WSTI_WD_INV_DOC_ITEM TYPE STANDARD TABLE OF WSTI_WD_INV_DOC_ITEM,
      WA_WSTI_WD_INV_DOC_ITEM TYPE WSTI_WD_INV_DOC_ITEM,
      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: <WSTI_WD_INV_DOC_ITEM> TYPE WSTI_WD_INV_DOC_ITEM.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM WSTI_WD_INV_DOC_ITEM
*  INTO TABLE @DATA(IT_WSTI_WD_INV_DOC_ITEM2).
*--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_WSTI_WD_INV_DOC_ITEM INDEX 1 INTO DATA(WA_WSTI_WD_INV_DOC_ITEM2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_WSTI_WD_INV_DOC_ITEM ASSIGNING <WSTI_WD_INV_DOC_ITEM>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<WSTI_WD_INV_DOC_ITEM>-ZEILI = 1.
<WSTI_WD_INV_DOC_ITEM>-MATNR = 1.
<WSTI_WD_INV_DOC_ITEM>-CHARG = 1.
<WSTI_WD_INV_DOC_ITEM>-BSTAR = 1.
<WSTI_WD_INV_DOC_ITEM>-KDAUF = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_WSTI_WD_INV_DOC_ITEM-KDPOS, sy-vline,
WA_WSTI_WD_INV_DOC_ITEM-KDEIN, sy-vline,
WA_WSTI_WD_INV_DOC_ITEM-LIFNR, sy-vline,
WA_WSTI_WD_INV_DOC_ITEM-KUNNR, sy-vline,
WA_WSTI_WD_INV_DOC_ITEM-PLPLA, sy-vline,
WA_WSTI_WD_INV_DOC_ITEM-XZAEL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_WSTI_WD_INV_DOC_ITEM 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_WSTI_WD_INV_DOC_ITEM 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_WSTI_WD_INV_DOC_ITEM INTO WA_WSTI_WD_INV_DOC_ITEM. *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 MATNR CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = WA_WSTI_WD_INV_DOC_ITEM-MATNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_WSTI_WD_INV_DOC_ITEM-MATNR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

*Conversion exit EAN11, internal->external for field EAN11 CALL FUNCTION 'CONVERSION_EXIT_EAN11_OUTPUT' EXPORTING input = WA_WSTI_WD_INV_DOC_ITEM-EAN11 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_WSTI_WD_INV_DOC_ITEM-EAN11.
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_WSTI_WD_INV_DOC_ITEM_STR,
ZEILI TYPE STRING,
MATNR TYPE STRING,
CHARG TYPE STRING,
BSTAR TYPE STRING,
KDAUF TYPE STRING,
KDPOS TYPE STRING,
KDEIN TYPE STRING,
LIFNR TYPE STRING,
KUNNR TYPE STRING,
PLPLA TYPE STRING,
XZAEL TYPE STRING,
XNULL TYPE STRING,
MENGE TYPE STRING,
MEINS TYPE STRING,
ERFMG TYPE STRING,
ERFME TYPE STRING,
WAERS TYPE STRING,
PS_PSP_PNR TYPE STRING,
EXVKW TYPE STRING,
KWART TYPE STRING,
GRUND TYPE STRING,
WSTI_COUNTDATE TYPE STRING,
WSTI_COUNTTIME TYPE STRING,
WSTI_FREEZEDATE TYPE STRING,
WSTI_FREEZETIME TYPE STRING,
WSTI_POSM TYPE STRING,
WSTI_POSW TYPE STRING,
WSTI_XCALC TYPE STRING,
WSTI_ENTERDATE TYPE STRING,
WSTI_ENTERTIME TYPE STRING,
ERFMG_CHAR TYPE STRING,
EXVKW_CHAR TYPE STRING,
DIFWT_POSWS TYPE STRING,
MAKTX TYPE STRING,
PRUEFLOS TYPE STRING,
POSWS TYPE STRING,
EXVKW_POSWS TYPE STRING,
DIFMG_ERFME TYPE STRING,
BUCHM_ERFME TYPE STRING,
BUCHW_POSWS TYPE STRING,
DMBTR_POSWS TYPE STRING,
STEXT TYPE STRING,
DIFMG_ERFME_AMNT TYPE STRING,
DIFWT_POSWS_AMNT TYPE STRING,
MAT_KEY TYPE STRING,
EAN11 TYPE STRING,
BTEXT TYPE STRING,
DIFF_ACTION TYPE STRING,
XCALC_TXT TYPE STRING,
SALV_WD_TABLE_INDEX TYPE STRING,
ERFMG_R TYPE STRING,
ERFME_R TYPE STRING,
EXVKW_POSWS_R TYPE STRING,
GRUND_R TYPE STRING,
ERFME_VALUE_SET TYPE STRING,
GRUND_VALUE_SET TYPE STRING,
DIFWT_POSWS_CO TYPE STRING,
DIFMG_ERFME_CO TYPE STRING,
DMBTR_POSWS_CO TYPE STRING,END OF T_EKKO_STR. DATA: WA_WSTI_WD_INV_DOC_ITEM_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_WSTI_WD_INV_DOC_ITEM_STR-ZEILI sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-MATNR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-CHARG sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-BSTAR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-KDAUF sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-KDPOS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-KDEIN sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-LIFNR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-KUNNR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-PLPLA sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-XZAEL sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-XNULL sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-MENGE sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-MEINS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-ERFMG sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-ERFME sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WAERS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-PS_PSP_PNR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-EXVKW sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-KWART sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-GRUND sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_COUNTDATE sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_COUNTTIME sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_FREEZEDATE sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_FREEZETIME sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_POSM sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_POSW sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_XCALC sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_ENTERDATE sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-WSTI_ENTERTIME sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-ERFMG_CHAR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-EXVKW_CHAR sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFWT_POSWS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-MAKTX sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-PRUEFLOS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-POSWS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-EXVKW_POSWS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFMG_ERFME sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-BUCHM_ERFME sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-BUCHW_POSWS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DMBTR_POSWS sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-STEXT sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFMG_ERFME_AMNT sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFWT_POSWS_AMNT sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-MAT_KEY sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-EAN11 sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-BTEXT sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFF_ACTION sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-XCALC_TXT sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-SALV_WD_TABLE_INDEX sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-ERFMG_R sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-ERFME_R sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-EXVKW_POSWS_R sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-GRUND_R sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-ERFME_VALUE_SET sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-GRUND_VALUE_SET sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFWT_POSWS_CO sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DIFMG_ERFME_CO sy-vline
WA_WSTI_WD_INV_DOC_ITEM_STR-DMBTR_POSWS_CO sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.