ABAP Select data from SAP table WTYF4_VBAP 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 WTYF4_VBAP 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 WTYF4_VBAP. 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 WTYF4_VBAP 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_WTYF4_VBAP TYPE STANDARD TABLE OF WTYF4_VBAP,
      WA_WTYF4_VBAP TYPE WTYF4_VBAP,
      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: <WTYF4_VBAP> TYPE WTYF4_VBAP.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM WTYF4_VBAP
*  INTO TABLE @DATA(IT_WTYF4_VBAP2).
*--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_WTYF4_VBAP INDEX 1 INTO DATA(WA_WTYF4_VBAP2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_WTYF4_VBAP ASSIGNING <WTYF4_VBAP>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<WTYF4_VBAP>-MANDT = 1.
<WTYF4_VBAP>-VBELN = 1.
<WTYF4_VBAP>-POSNR = 1.
<WTYF4_VBAP>-AUART = 1.
<WTYF4_VBAP>-VBTYP = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_WTYF4_VBAP-AUGRU, sy-vline,
WA_WTYF4_VBAP-ERNAM, sy-vline,
WA_WTYF4_VBAP-SUBMI, sy-vline,
WA_WTYF4_VBAP-FAKSK, sy-vline,
WA_WTYF4_VBAP-NETWR, sy-vline,
WA_WTYF4_VBAP-WAERK, sy-vline.
ENDLOOP. *Add any further fields from structure WA_WTYF4_VBAP 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_WTYF4_VBAP 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_WTYF4_VBAP INTO WA_WTYF4_VBAP. *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 VBELN CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_WTYF4_VBAP-VBELN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_WTYF4_VBAP-VBELN.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AUART, internal->external for field AUART CALL FUNCTION 'CONVERSION_EXIT_AUART_OUTPUT' EXPORTING input = WA_WTYF4_VBAP-AUART IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_WTYF4_VBAP-AUART.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

*Conversion exit EAN11, internal->external for field EAN11 CALL FUNCTION 'CONVERSION_EXIT_EAN11_OUTPUT' EXPORTING input = WA_WTYF4_VBAP-EAN11 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_WTYF4_VBAP-EAN11.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PRCTR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_WTYF4_VBAP-PRCTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_WTYF4_VBAP-PRCTR.
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_WTYF4_VBAP_STR,
MANDT TYPE STRING,
VBELN TYPE STRING,
POSNR TYPE STRING,
AUART TYPE STRING,
VBTYP TYPE STRING,
AUGRU TYPE STRING,
ERNAM TYPE STRING,
SUBMI TYPE STRING,
FAKSK TYPE STRING,
NETWR TYPE STRING,
WAERK TYPE STRING,
VKORG TYPE STRING,
VTWEG TYPE STRING,
VKGRP TYPE STRING,
VKBUR TYPE STRING,
BSTZD TYPE STRING,
BNAME TYPE STRING,
TELF1 TYPE STRING,
KUNNR TYPE STRING,
KVGR1 TYPE STRING,
KVGR2 TYPE STRING,
KVGR3 TYPE STRING,
KVGR4 TYPE STRING,
KVGR5 TYPE STRING,
MATNRX TYPE STRING,
MATWA TYPE STRING,
CHARG TYPE STRING,
MATKL TYPE STRING,
ARKTX TYPE STRING,
PSTYV TYPE STRING,
UEPOS TYPE STRING,
ABGRU TYPE STRING,
PRODH TYPE STRING,
ZMENG TYPE STRING,
ZIEME TYPE STRING,
UMZIZ TYPE STRING,
UMZIN TYPE STRING,
MEINS TYPE STRING,
KDMAT TYPE STRING,
POSEX TYPE STRING,
FAKSP TYPE STRING,
SPART TYPE STRING,
NETWRX TYPE STRING,
VGBEL TYPE STRING,
VGPOS TYPE STRING,
WERKS TYPE STRING,
NETPR TYPE STRING,
EAN11 TYPE STRING,
MVGR1 TYPE STRING,
MVGR2 TYPE STRING,
MVGR3 TYPE STRING,
MVGR4 TYPE STRING,
MVGR5 TYPE STRING,
PRCTR TYPE STRING,
CUOBJ TYPE STRING,
WAERKX TYPE STRING,
KTEXT TYPE STRING,END OF T_EKKO_STR. DATA: WA_WTYF4_VBAP_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_WTYF4_VBAP_STR-MANDT sy-vline
WA_WTYF4_VBAP_STR-VBELN sy-vline
WA_WTYF4_VBAP_STR-POSNR sy-vline
WA_WTYF4_VBAP_STR-AUART sy-vline
WA_WTYF4_VBAP_STR-VBTYP sy-vline
WA_WTYF4_VBAP_STR-AUGRU sy-vline
WA_WTYF4_VBAP_STR-ERNAM sy-vline
WA_WTYF4_VBAP_STR-SUBMI sy-vline
WA_WTYF4_VBAP_STR-FAKSK sy-vline
WA_WTYF4_VBAP_STR-NETWR sy-vline
WA_WTYF4_VBAP_STR-WAERK sy-vline
WA_WTYF4_VBAP_STR-VKORG sy-vline
WA_WTYF4_VBAP_STR-VTWEG sy-vline
WA_WTYF4_VBAP_STR-VKGRP sy-vline
WA_WTYF4_VBAP_STR-VKBUR sy-vline
WA_WTYF4_VBAP_STR-BSTZD sy-vline
WA_WTYF4_VBAP_STR-BNAME sy-vline
WA_WTYF4_VBAP_STR-TELF1 sy-vline
WA_WTYF4_VBAP_STR-KUNNR sy-vline
WA_WTYF4_VBAP_STR-KVGR1 sy-vline
WA_WTYF4_VBAP_STR-KVGR2 sy-vline
WA_WTYF4_VBAP_STR-KVGR3 sy-vline
WA_WTYF4_VBAP_STR-KVGR4 sy-vline
WA_WTYF4_VBAP_STR-KVGR5 sy-vline
WA_WTYF4_VBAP_STR-MATNRX sy-vline
WA_WTYF4_VBAP_STR-MATWA sy-vline
WA_WTYF4_VBAP_STR-CHARG sy-vline
WA_WTYF4_VBAP_STR-MATKL sy-vline
WA_WTYF4_VBAP_STR-ARKTX sy-vline
WA_WTYF4_VBAP_STR-PSTYV sy-vline
WA_WTYF4_VBAP_STR-UEPOS sy-vline
WA_WTYF4_VBAP_STR-ABGRU sy-vline
WA_WTYF4_VBAP_STR-PRODH sy-vline
WA_WTYF4_VBAP_STR-ZMENG sy-vline
WA_WTYF4_VBAP_STR-ZIEME sy-vline
WA_WTYF4_VBAP_STR-UMZIZ sy-vline
WA_WTYF4_VBAP_STR-UMZIN sy-vline
WA_WTYF4_VBAP_STR-MEINS sy-vline
WA_WTYF4_VBAP_STR-KDMAT sy-vline
WA_WTYF4_VBAP_STR-POSEX sy-vline
WA_WTYF4_VBAP_STR-FAKSP sy-vline
WA_WTYF4_VBAP_STR-SPART sy-vline
WA_WTYF4_VBAP_STR-NETWRX sy-vline
WA_WTYF4_VBAP_STR-VGBEL sy-vline
WA_WTYF4_VBAP_STR-VGPOS sy-vline
WA_WTYF4_VBAP_STR-WERKS sy-vline
WA_WTYF4_VBAP_STR-NETPR sy-vline
WA_WTYF4_VBAP_STR-EAN11 sy-vline
WA_WTYF4_VBAP_STR-MVGR1 sy-vline
WA_WTYF4_VBAP_STR-MVGR2 sy-vline
WA_WTYF4_VBAP_STR-MVGR3 sy-vline
WA_WTYF4_VBAP_STR-MVGR4 sy-vline
WA_WTYF4_VBAP_STR-MVGR5 sy-vline
WA_WTYF4_VBAP_STR-PRCTR sy-vline
WA_WTYF4_VBAP_STR-CUOBJ sy-vline
WA_WTYF4_VBAP_STR-WAERKX sy-vline
WA_WTYF4_VBAP_STR-KTEXT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.