ABAP Select data from SAP table VBKD 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 VBKD 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 VBKD. 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 VBKD 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_VBKD TYPE STANDARD TABLE OF VBKD,
      WA_VBKD TYPE VBKD,
      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: <VBKD> TYPE VBKD.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM VBKD
*  INTO TABLE @DATA(IT_VBKD2).
*--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_VBKD INDEX 1 INTO DATA(WA_VBKD2).


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

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_VBKD-BZIRK, sy-vline,
WA_VBKD-PLTYP, sy-vline,
WA_VBKD-INCO1, sy-vline,
WA_VBKD-INCO2, sy-vline,
WA_VBKD-KZAZU, sy-vline,
WA_VBKD-PERFK, sy-vline.
ENDLOOP. *Add any further fields from structure WA_VBKD 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_VBKD 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_VBKD INTO WA_VBKD. *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_VBKD-VBELN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VBKD-VBELN.
WRITE:/ 'New Value:', ld_input.

*Conversion exit EXCRT, internal->external for field KURSK CALL FUNCTION 'CONVERSION_EXIT_EXCRT_OUTPUT' EXPORTING input = WA_VBKD-KURSK IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VBKD-KURSK.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit EXCRT, internal->external for field STCUR CALL FUNCTION 'CONVERSION_EXIT_EXCRT_OUTPUT' EXPORTING input = WA_VBKD-STCUR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VBKD-STCUR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field VTREF CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VBKD-VTREF IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VBKD-VTREF.
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_VBKD_STR,
MANDT TYPE STRING,
VBELN TYPE STRING,
POSNR TYPE STRING,
KONDA TYPE STRING,
KDGRP TYPE STRING,
BZIRK TYPE STRING,
PLTYP TYPE STRING,
INCO1 TYPE STRING,
INCO2 TYPE STRING,
KZAZU TYPE STRING,
PERFK TYPE STRING,
PERRL TYPE STRING,
MRNKZ TYPE STRING,
KURRF TYPE STRING,
VALTG TYPE STRING,
VALDT TYPE STRING,
KTGRD TYPE STRING,
KURSK TYPE STRING,
PRSDT TYPE STRING,
FKDAT TYPE STRING,
FBUDA TYPE STRING,
GJAHR TYPE STRING,
POPER TYPE STRING,
STCUR TYPE STRING,
MSCHL TYPE STRING,
MANSP TYPE STRING,
FPLNR TYPE STRING,
WAKTION TYPE STRING,
ABSSC TYPE STRING,
LCNUM TYPE STRING,
J_1AFITP TYPE STRING,
J_1ARFZ TYPE STRING,
J_1AREGIO TYPE STRING,
J_1AGICD TYPE STRING,
J_1ADTYP TYPE STRING,
J_1ATXREL TYPE STRING,
ABTNR TYPE STRING,
EMPST TYPE STRING,
BSTKD TYPE STRING,
BSTDK TYPE STRING,
BSARK TYPE STRING,
IHREZ TYPE STRING,
BSTKD_E TYPE STRING,
BSTDK_E TYPE STRING,
BSARK_E TYPE STRING,
IHREZ_E TYPE STRING,
POSEX_E TYPE STRING,
KURSK_DAT TYPE STRING,
KURRF_DAT TYPE STRING,
KDKG1 TYPE STRING,
KDKG2 TYPE STRING,
KDKG3 TYPE STRING,
KDKG4 TYPE STRING,
KDKG5 TYPE STRING,
WKWAE TYPE STRING,
WKKUR TYPE STRING,
AKWAE TYPE STRING,
AKKUR TYPE STRING,
AKPRZ TYPE STRING,
J_1AINDXP TYPE STRING,
J_1AIDATEP TYPE STRING,
BSTKD_M TYPE STRING,
DELCO TYPE STRING,
FFPRF TYPE STRING,
BEMOT TYPE STRING,
FAKTF TYPE STRING,
RRREL TYPE STRING,
ACDATV TYPE STRING,
VSART TYPE STRING,
TRATY TYPE STRING,
TRMTYP TYPE STRING,
SDABW TYPE STRING,
WMINR TYPE STRING,
FKBER TYPE STRING,
PODKZ TYPE STRING,
CAMPAIGN TYPE STRING,
VKONT TYPE STRING,
DPBP_REF_FPLNR TYPE STRING,
DPBP_REF_FPLTR TYPE STRING,
REVSP TYPE STRING,
REVEVTYP TYPE STRING,
VTREF TYPE STRING,
BOOKING_FR TYPE STRING,
BOOKING_TO TYPE STRING,
SHIPPING_FR TYPE STRING,
SHIPPING_TO TYPE STRING,
BILLING_FR TYPE STRING,
BILLING_TO TYPE STRING,
EXERCISE_FR TYPE STRING,
EXERCISE_TO TYPE STRING,
DESIGN_REG_NO TYPE STRING,
DROP_SHIP TYPE STRING,
RFPLN TYPE STRING,
SYNCH TYPE STRING,
PACKNO TYPE STRING,
PEROP_BEG TYPE STRING,
PEROP_END TYPE STRING,
STCODE TYPE STRING,
FORMC1 TYPE STRING,
FORMC2 TYPE STRING,
STEUC TYPE STRING,
COMPREAS TYPE STRING,
MNDID TYPE STRING,
PAY_TYPE TYPE STRING,
SEPON TYPE STRING,
MNDVG TYPE STRING,END OF T_EKKO_STR. DATA: WA_VBKD_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_VBKD_STR-MANDT sy-vline
WA_VBKD_STR-VBELN sy-vline
WA_VBKD_STR-POSNR sy-vline
WA_VBKD_STR-KONDA sy-vline
WA_VBKD_STR-KDGRP sy-vline
WA_VBKD_STR-BZIRK sy-vline
WA_VBKD_STR-PLTYP sy-vline
WA_VBKD_STR-INCO1 sy-vline
WA_VBKD_STR-INCO2 sy-vline
WA_VBKD_STR-KZAZU sy-vline
WA_VBKD_STR-PERFK sy-vline
WA_VBKD_STR-PERRL sy-vline
WA_VBKD_STR-MRNKZ sy-vline
WA_VBKD_STR-KURRF sy-vline
WA_VBKD_STR-VALTG sy-vline
WA_VBKD_STR-VALDT sy-vline
WA_VBKD_STR-KTGRD sy-vline
WA_VBKD_STR-KURSK sy-vline
WA_VBKD_STR-PRSDT sy-vline
WA_VBKD_STR-FKDAT sy-vline
WA_VBKD_STR-FBUDA sy-vline
WA_VBKD_STR-GJAHR sy-vline
WA_VBKD_STR-POPER sy-vline
WA_VBKD_STR-STCUR sy-vline
WA_VBKD_STR-MSCHL sy-vline
WA_VBKD_STR-MANSP sy-vline
WA_VBKD_STR-FPLNR sy-vline
WA_VBKD_STR-WAKTION sy-vline
WA_VBKD_STR-ABSSC sy-vline
WA_VBKD_STR-LCNUM sy-vline
WA_VBKD_STR-J_1AFITP sy-vline
WA_VBKD_STR-J_1ARFZ sy-vline
WA_VBKD_STR-J_1AREGIO sy-vline
WA_VBKD_STR-J_1AGICD sy-vline
WA_VBKD_STR-J_1ADTYP sy-vline
WA_VBKD_STR-J_1ATXREL sy-vline
WA_VBKD_STR-ABTNR sy-vline
WA_VBKD_STR-EMPST sy-vline
WA_VBKD_STR-BSTKD sy-vline
WA_VBKD_STR-BSTDK sy-vline
WA_VBKD_STR-BSARK sy-vline
WA_VBKD_STR-IHREZ sy-vline
WA_VBKD_STR-BSTKD_E sy-vline
WA_VBKD_STR-BSTDK_E sy-vline
WA_VBKD_STR-BSARK_E sy-vline
WA_VBKD_STR-IHREZ_E sy-vline
WA_VBKD_STR-POSEX_E sy-vline
WA_VBKD_STR-KURSK_DAT sy-vline
WA_VBKD_STR-KURRF_DAT sy-vline
WA_VBKD_STR-KDKG1 sy-vline
WA_VBKD_STR-KDKG2 sy-vline
WA_VBKD_STR-KDKG3 sy-vline
WA_VBKD_STR-KDKG4 sy-vline
WA_VBKD_STR-KDKG5 sy-vline
WA_VBKD_STR-WKWAE sy-vline
WA_VBKD_STR-WKKUR sy-vline
WA_VBKD_STR-AKWAE sy-vline
WA_VBKD_STR-AKKUR sy-vline
WA_VBKD_STR-AKPRZ sy-vline
WA_VBKD_STR-J_1AINDXP sy-vline
WA_VBKD_STR-J_1AIDATEP sy-vline
WA_VBKD_STR-BSTKD_M sy-vline
WA_VBKD_STR-DELCO sy-vline
WA_VBKD_STR-FFPRF sy-vline
WA_VBKD_STR-BEMOT sy-vline
WA_VBKD_STR-FAKTF sy-vline
WA_VBKD_STR-RRREL sy-vline
WA_VBKD_STR-ACDATV sy-vline
WA_VBKD_STR-VSART sy-vline
WA_VBKD_STR-TRATY sy-vline
WA_VBKD_STR-TRMTYP sy-vline
WA_VBKD_STR-SDABW sy-vline
WA_VBKD_STR-WMINR sy-vline
WA_VBKD_STR-FKBER sy-vline
WA_VBKD_STR-PODKZ sy-vline
WA_VBKD_STR-CAMPAIGN sy-vline
WA_VBKD_STR-VKONT sy-vline
WA_VBKD_STR-DPBP_REF_FPLNR sy-vline
WA_VBKD_STR-DPBP_REF_FPLTR sy-vline
WA_VBKD_STR-REVSP sy-vline
WA_VBKD_STR-REVEVTYP sy-vline
WA_VBKD_STR-VTREF sy-vline
WA_VBKD_STR-BOOKING_FR sy-vline
WA_VBKD_STR-BOOKING_TO sy-vline
WA_VBKD_STR-SHIPPING_FR sy-vline
WA_VBKD_STR-SHIPPING_TO sy-vline
WA_VBKD_STR-BILLING_FR sy-vline
WA_VBKD_STR-BILLING_TO sy-vline
WA_VBKD_STR-EXERCISE_FR sy-vline
WA_VBKD_STR-EXERCISE_TO sy-vline
WA_VBKD_STR-DESIGN_REG_NO sy-vline
WA_VBKD_STR-DROP_SHIP sy-vline
WA_VBKD_STR-RFPLN sy-vline
WA_VBKD_STR-SYNCH sy-vline
WA_VBKD_STR-PACKNO sy-vline
WA_VBKD_STR-PEROP_BEG sy-vline
WA_VBKD_STR-PEROP_END sy-vline
WA_VBKD_STR-STCODE sy-vline
WA_VBKD_STR-FORMC1 sy-vline
WA_VBKD_STR-FORMC2 sy-vline
WA_VBKD_STR-STEUC sy-vline
WA_VBKD_STR-COMPREAS sy-vline
WA_VBKD_STR-MNDID sy-vline
WA_VBKD_STR-PAY_TYPE sy-vline
WA_VBKD_STR-SEPON sy-vline
WA_VBKD_STR-MNDVG sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.