ABAP Select data from SAP table DKKOP 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 DKKOP 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 DKKOP. 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 DKKOP 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_DKKOP TYPE STANDARD TABLE OF DKKOP, WA_DKKOP TYPE DKKOP, 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: <DKKOP> TYPE DKKOP. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_DKKOP. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM DKKOP INTO TABLE IT_DKKOP. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM DKKOP * INTO TABLE @DATA(IT_DKKOP2). *--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_DKKOP INDEX 1 INTO DATA(WA_DKKOP2). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_DKKOP ASSIGNING <DKKOP>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<DKKOP>-MANDT = 1.
<DKKOP>-KOART = 1.
<DKKOP>-BUKRS = 1.
<DKKOP>-KONTO = 1.
<DKKOP>-GJAHR = 1.
ENDLOOP. LOOP AT IT_DKKOP INTO WA_DKKOP. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_DKKOP-GSBER, sy-vline,
WA_DKKOP-SAART, sy-vline,
WA_DKKOP-UMSKS, sy-vline,
WA_DKKOP-UMSAT, sy-vline,
WA_DKKOP-BUPER, sy-vline,
WA_DKKOP-BUDAT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_DKKOP 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_DKKOP 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_DKKOP INTO WA_DKKOP. *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 KONTO CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-KONTO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-KONTO.
WRITE:/ 'New Value:', ld_input.
*Conversion exit GJAHR, internal->external for field GJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_DKKOP-GJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-GJAHR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field BELNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-BELNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-BELNR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field HKONT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-HKONT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-HKONT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field ALTKT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-ALTKT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-ALTKT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field DBBLG CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-DBBLG IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-DBBLG.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field DMBTR CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-DMBTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-DMBTR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field WRBTR CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-WRBTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-WRBTR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field WSKTO CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-WSKTO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-WSKTO.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field WMWST CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-WMWST IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-WMWST.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AC132, internal->external for field MWSHB CALL FUNCTION 'CONVERSION_EXIT_AC132_OUTPUT' EXPORTING input = WA_DKKOP-MWSHB IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-MWSHB.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field AUGBL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-AUGBL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-AUGBL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field SKFBT CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-SKFBT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-SKFBT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field MWSTS CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-MWSTS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-MWSTS.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AC132, internal->external for field MDSHB CALL FUNCTION 'CONVERSION_EXIT_AC132_OUTPUT' EXPORTING input = WA_DKKOP-MDSHB IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-MDSHB.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field SKNTO CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-SKNTO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-SKNTO.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field HWBAS CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-HWBAS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-HWBAS.
WRITE:/ 'New Value:', ld_input.
*Conversion exit AU132, internal->external for field FWBAS CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_DKKOP-FWBAS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-FWBAS.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field KOSTL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-KOSTL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-KOSTL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field GKONT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-GKONT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-GKONT.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field VBUND CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_DKKOP-VBUND IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_DKKOP-VBUND.
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_DKKOP_STR,
MANDT TYPE STRING,
KOART TYPE STRING,
BUKRS TYPE STRING,
KONTO TYPE STRING,
GJAHR TYPE STRING,
GSBER TYPE STRING,
SAART TYPE STRING,
UMSKS TYPE STRING,
UMSAT TYPE STRING,
BUPER TYPE STRING,
BUDAT TYPE STRING,
BELNR TYPE STRING,
BUZEI TYPE STRING,
HKONT TYPE STRING,
ALTKZ TYPE STRING,
ALTKT TYPE STRING,
BLART TYPE STRING,
BLDAT TYPE STRING,
UMSKZ TYPE STRING,
WAERS TYPE STRING,
XBLNR TYPE STRING,
CPUDT TYPE STRING,
USNAM TYPE STRING,
STAKZ TYPE STRING,
DBBLG TYPE STRING,
BKTXT TYPE STRING,
XUMSW TYPE STRING,
XOPVW TYPE STRING,
BSCHL TYPE STRING,
SHKZG TYPE STRING,
BUZID TYPE STRING,
MWSKZ TYPE STRING,
FIPLS TYPE STRING,
DMBTR TYPE STRING,
GSBE1 TYPE STRING,
WRBTR TYPE STRING,
WSKTO TYPE STRING,
WMWST TYPE STRING,
MWSHB TYPE STRING,
ZUONR TYPE STRING,
AUGBL TYPE STRING,
AUGDT TYPE STRING,
AUGCP TYPE STRING,
MSCHL TYPE STRING,
ZLSCH TYPE STRING,
NETDT TYPE STRING,
ZFBDT TYPE STRING,
SKFBT TYPE STRING,
MWSTS TYPE STRING,
MDSHB TYPE STRING,
SKNTO TYPE STRING,
HWBAS TYPE STRING,
ZINKZ TYPE STRING,
VALUT TYPE STRING,
FWBAS TYPE STRING,
PERNR TYPE STRING,
ABPER TYPE STRING,
KOSTL TYPE STRING,
SGTXT TYPE STRING,
GKART TYPE STRING,
GKONT TYPE STRING,
VBUND TYPE STRING,
EGMLD TYPE STRING,
STCEG TYPE STRING,
BVORG TYPE STRING,
XNEGP TYPE STRING,END OF T_EKKO_STR. DATA: WA_DKKOP_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_DKKOP_STR-MANDT sy-vline
WA_DKKOP_STR-KOART sy-vline
WA_DKKOP_STR-BUKRS sy-vline
WA_DKKOP_STR-KONTO sy-vline
WA_DKKOP_STR-GJAHR sy-vline
WA_DKKOP_STR-GSBER sy-vline
WA_DKKOP_STR-SAART sy-vline
WA_DKKOP_STR-UMSKS sy-vline
WA_DKKOP_STR-UMSAT sy-vline
WA_DKKOP_STR-BUPER sy-vline
WA_DKKOP_STR-BUDAT sy-vline
WA_DKKOP_STR-BELNR sy-vline
WA_DKKOP_STR-BUZEI sy-vline
WA_DKKOP_STR-HKONT sy-vline
WA_DKKOP_STR-ALTKZ sy-vline
WA_DKKOP_STR-ALTKT sy-vline
WA_DKKOP_STR-BLART sy-vline
WA_DKKOP_STR-BLDAT sy-vline
WA_DKKOP_STR-UMSKZ sy-vline
WA_DKKOP_STR-WAERS sy-vline
WA_DKKOP_STR-XBLNR sy-vline
WA_DKKOP_STR-CPUDT sy-vline
WA_DKKOP_STR-USNAM sy-vline
WA_DKKOP_STR-STAKZ sy-vline
WA_DKKOP_STR-DBBLG sy-vline
WA_DKKOP_STR-BKTXT sy-vline
WA_DKKOP_STR-XUMSW sy-vline
WA_DKKOP_STR-XOPVW sy-vline
WA_DKKOP_STR-BSCHL sy-vline
WA_DKKOP_STR-SHKZG sy-vline
WA_DKKOP_STR-BUZID sy-vline
WA_DKKOP_STR-MWSKZ sy-vline
WA_DKKOP_STR-FIPLS sy-vline
WA_DKKOP_STR-DMBTR sy-vline
WA_DKKOP_STR-GSBE1 sy-vline
WA_DKKOP_STR-WRBTR sy-vline
WA_DKKOP_STR-WSKTO sy-vline
WA_DKKOP_STR-WMWST sy-vline
WA_DKKOP_STR-MWSHB sy-vline
WA_DKKOP_STR-ZUONR sy-vline
WA_DKKOP_STR-AUGBL sy-vline
WA_DKKOP_STR-AUGDT sy-vline
WA_DKKOP_STR-AUGCP sy-vline
WA_DKKOP_STR-MSCHL sy-vline
WA_DKKOP_STR-ZLSCH sy-vline
WA_DKKOP_STR-NETDT sy-vline
WA_DKKOP_STR-ZFBDT sy-vline
WA_DKKOP_STR-SKFBT sy-vline
WA_DKKOP_STR-MWSTS sy-vline
WA_DKKOP_STR-MDSHB sy-vline
WA_DKKOP_STR-SKNTO sy-vline
WA_DKKOP_STR-HWBAS sy-vline
WA_DKKOP_STR-ZINKZ sy-vline
WA_DKKOP_STR-VALUT sy-vline
WA_DKKOP_STR-FWBAS sy-vline
WA_DKKOP_STR-PERNR sy-vline
WA_DKKOP_STR-ABPER sy-vline
WA_DKKOP_STR-KOSTL sy-vline
WA_DKKOP_STR-SGTXT sy-vline
WA_DKKOP_STR-GKART sy-vline
WA_DKKOP_STR-GKONT sy-vline
WA_DKKOP_STR-VBUND sy-vline
WA_DKKOP_STR-EGMLD sy-vline
WA_DKKOP_STR-STCEG sy-vline
WA_DKKOP_STR-BVORG sy-vline
WA_DKKOP_STR-XNEGP sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.