ABAP Select data from SAP table VBUK 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 VBUK 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 VBUK. 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 VBUK 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_VBUK TYPE STANDARD TABLE OF VBUK,
      WA_VBUK TYPE VBUK,
      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: <VBUK> TYPE VBUK.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM VBUK
*  INTO TABLE @DATA(IT_VBUK2).
*--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_VBUK INDEX 1 INTO DATA(WA_VBUK2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_VBUK ASSIGNING <VBUK>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<VBUK>-MANDT = 1.
<VBUK>-VBELN = 1.
<VBUK>-RFSTK = 1.
<VBUK>-RFGSK = 1.
<VBUK>-BESTK = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_VBUK-LFSTK, sy-vline,
WA_VBUK-LFGSK, sy-vline,
WA_VBUK-WBSTK, sy-vline,
WA_VBUK-FKSTK, sy-vline,
WA_VBUK-FKSAK, sy-vline,
WA_VBUK-BUCHK, sy-vline.
ENDLOOP. *Add any further fields from structure WA_VBUK 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_VBUK 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_VBUK INTO WA_VBUK. *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_VBUK-VBELN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VBUK-VBELN.
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_VBUK_STR,
MANDT TYPE STRING,
VBELN TYPE STRING,
RFSTK TYPE STRING,
RFGSK TYPE STRING,
BESTK TYPE STRING,
LFSTK TYPE STRING,
LFGSK TYPE STRING,
WBSTK TYPE STRING,
FKSTK TYPE STRING,
FKSAK TYPE STRING,
BUCHK TYPE STRING,
ABSTK TYPE STRING,
GBSTK TYPE STRING,
KOSTK TYPE STRING,
LVSTK TYPE STRING,
UVALS TYPE STRING,
UVVLS TYPE STRING,
UVFAS TYPE STRING,
UVALL TYPE STRING,
UVVLK TYPE STRING,
UVFAK TYPE STRING,
UVPRS TYPE STRING,
VBTYP TYPE STRING,
VBOBJ TYPE STRING,
AEDAT TYPE STRING,
FKIVK TYPE STRING,
RELIK TYPE STRING,
UVK01 TYPE STRING,
UVK02 TYPE STRING,
UVK03 TYPE STRING,
UVK04 TYPE STRING,
UVK05 TYPE STRING,
UVS01 TYPE STRING,
UVS02 TYPE STRING,
UVS03 TYPE STRING,
UVS04 TYPE STRING,
UVS05 TYPE STRING,
PKSTK TYPE STRING,
CMPSA TYPE STRING,
CMPSB TYPE STRING,
CMPSC TYPE STRING,
CMPSD TYPE STRING,
CMPSE TYPE STRING,
CMPSF TYPE STRING,
CMPSG TYPE STRING,
CMPSH TYPE STRING,
CMPSI TYPE STRING,
CMPSJ TYPE STRING,
CMPSK TYPE STRING,
CMPSL TYPE STRING,
CMPS0 TYPE STRING,
CMPS1 TYPE STRING,
CMPS2 TYPE STRING,
CMGST TYPE STRING,
TRSTA TYPE STRING,
KOQUK TYPE STRING,
COSTA TYPE STRING,
SAPRL TYPE STRING,
UVPAS TYPE STRING,
UVPIS TYPE STRING,
UVWAS TYPE STRING,
UVPAK TYPE STRING,
UVPIK TYPE STRING,
UVWAK TYPE STRING,
UVGEK TYPE STRING,
CMPSM TYPE STRING,
DCSTK TYPE STRING,
VESTK TYPE STRING,
VLSTK TYPE STRING,
RRSTA TYPE STRING,
BLOCK TYPE STRING,
FSSTK TYPE STRING,
LSSTK TYPE STRING,
SPSTG TYPE STRING,
PDSTK TYPE STRING,
FMSTK TYPE STRING,
MANEK TYPE STRING,
SPE_TMPID TYPE STRING,
HDALL TYPE STRING,
HDALS TYPE STRING,
CMPS_CM TYPE STRING,
CMPS_TE TYPE STRING,
VBTYP_EXT TYPE STRING,
/SAPMP/LBASK TYPE STRING,
PODVWK TYPE STRING,END OF T_EKKO_STR. DATA: WA_VBUK_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_VBUK_STR-MANDT sy-vline
WA_VBUK_STR-VBELN sy-vline
WA_VBUK_STR-RFSTK sy-vline
WA_VBUK_STR-RFGSK sy-vline
WA_VBUK_STR-BESTK sy-vline
WA_VBUK_STR-LFSTK sy-vline
WA_VBUK_STR-LFGSK sy-vline
WA_VBUK_STR-WBSTK sy-vline
WA_VBUK_STR-FKSTK sy-vline
WA_VBUK_STR-FKSAK sy-vline
WA_VBUK_STR-BUCHK sy-vline
WA_VBUK_STR-ABSTK sy-vline
WA_VBUK_STR-GBSTK sy-vline
WA_VBUK_STR-KOSTK sy-vline
WA_VBUK_STR-LVSTK sy-vline
WA_VBUK_STR-UVALS sy-vline
WA_VBUK_STR-UVVLS sy-vline
WA_VBUK_STR-UVFAS sy-vline
WA_VBUK_STR-UVALL sy-vline
WA_VBUK_STR-UVVLK sy-vline
WA_VBUK_STR-UVFAK sy-vline
WA_VBUK_STR-UVPRS sy-vline
WA_VBUK_STR-VBTYP sy-vline
WA_VBUK_STR-VBOBJ sy-vline
WA_VBUK_STR-AEDAT sy-vline
WA_VBUK_STR-FKIVK sy-vline
WA_VBUK_STR-RELIK sy-vline
WA_VBUK_STR-UVK01 sy-vline
WA_VBUK_STR-UVK02 sy-vline
WA_VBUK_STR-UVK03 sy-vline
WA_VBUK_STR-UVK04 sy-vline
WA_VBUK_STR-UVK05 sy-vline
WA_VBUK_STR-UVS01 sy-vline
WA_VBUK_STR-UVS02 sy-vline
WA_VBUK_STR-UVS03 sy-vline
WA_VBUK_STR-UVS04 sy-vline
WA_VBUK_STR-UVS05 sy-vline
WA_VBUK_STR-PKSTK sy-vline
WA_VBUK_STR-CMPSA sy-vline
WA_VBUK_STR-CMPSB sy-vline
WA_VBUK_STR-CMPSC sy-vline
WA_VBUK_STR-CMPSD sy-vline
WA_VBUK_STR-CMPSE sy-vline
WA_VBUK_STR-CMPSF sy-vline
WA_VBUK_STR-CMPSG sy-vline
WA_VBUK_STR-CMPSH sy-vline
WA_VBUK_STR-CMPSI sy-vline
WA_VBUK_STR-CMPSJ sy-vline
WA_VBUK_STR-CMPSK sy-vline
WA_VBUK_STR-CMPSL sy-vline
WA_VBUK_STR-CMPS0 sy-vline
WA_VBUK_STR-CMPS1 sy-vline
WA_VBUK_STR-CMPS2 sy-vline
WA_VBUK_STR-CMGST sy-vline
WA_VBUK_STR-TRSTA sy-vline
WA_VBUK_STR-KOQUK sy-vline
WA_VBUK_STR-COSTA sy-vline
WA_VBUK_STR-SAPRL sy-vline
WA_VBUK_STR-UVPAS sy-vline
WA_VBUK_STR-UVPIS sy-vline
WA_VBUK_STR-UVWAS sy-vline
WA_VBUK_STR-UVPAK sy-vline
WA_VBUK_STR-UVPIK sy-vline
WA_VBUK_STR-UVWAK sy-vline
WA_VBUK_STR-UVGEK sy-vline
WA_VBUK_STR-CMPSM sy-vline
WA_VBUK_STR-DCSTK sy-vline
WA_VBUK_STR-VESTK sy-vline
WA_VBUK_STR-VLSTK sy-vline
WA_VBUK_STR-RRSTA sy-vline
WA_VBUK_STR-BLOCK sy-vline
WA_VBUK_STR-FSSTK sy-vline
WA_VBUK_STR-LSSTK sy-vline
WA_VBUK_STR-SPSTG sy-vline
WA_VBUK_STR-PDSTK sy-vline
WA_VBUK_STR-FMSTK sy-vline
WA_VBUK_STR-MANEK sy-vline
WA_VBUK_STR-SPE_TMPID sy-vline
WA_VBUK_STR-HDALL sy-vline
WA_VBUK_STR-HDALS sy-vline
WA_VBUK_STR-CMPS_CM sy-vline
WA_VBUK_STR-CMPS_TE sy-vline
WA_VBUK_STR-VBTYP_EXT sy-vline
WA_VBUK_STR-/SAPMP/LBASK sy-vline
WA_VBUK_STR-PODVWK sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.