ABAP Select data from SAP table KCSCOFP 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 KCSCOFP 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 KCSCOFP. 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 KCSCOFP 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_KCSCOFP TYPE STANDARD TABLE OF KCSCOFP,
      WA_KCSCOFP TYPE KCSCOFP,
      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: <KCSCOFP> TYPE KCSCOFP.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM KCSCOFP
*  INTO TABLE @DATA(IT_KCSCOFP2).
*--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_KCSCOFP INDEX 1 INTO DATA(WA_KCSCOFP2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_KCSCOFP ASSIGNING <KCSCOFP>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<KCSCOFP>-MANDT = 1.
<KCSCOFP>-KOKRS = 1.
<KCSCOFP>-BELNR = 1.
<KCSCOFP>-BUZEI = 1.
<KCSCOFP>-TWAER = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_KCSCOFP-GJAHR, sy-vline,
WA_KCSCOFP-PERIO, sy-vline,
WA_KCSCOFP-WRTTP, sy-vline,
WA_KCSCOFP-VRGNG, sy-vline,
WA_KCSCOFP-POSIT, sy-vline,
WA_KCSCOFP-OBJNR, sy-vline.
ENDLOOP. *Add any further fields from structure WA_KCSCOFP 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_KCSCOFP 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_KCSCOFP INTO WA_KCSCOFP. *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 BELNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KCSCOFP-BELNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCSCOFP-BELNR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

*Conversion exit NUMCV, internal->external for field LEDNR CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_KCSCOFP-LEDNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCSCOFP-LEDNR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ALPHA, internal->external for field VBELN CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KCSCOFP-VBELN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCSCOFP-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_KCSCOFP_STR,
MANDT TYPE STRING,
KOKRS TYPE STRING,
BELNR TYPE STRING,
BUZEI TYPE STRING,
TWAER TYPE STRING,
GJAHR TYPE STRING,
PERIO TYPE STRING,
WRTTP TYPE STRING,
VRGNG TYPE STRING,
POSIT TYPE STRING,
OBJNR TYPE STRING,
HKONT TYPE STRING,
WKGBTR TYPE STRING,
WKGBTRO TYPE STRING,
WTGBTR TYPE STRING,
WTGBTRO TYPE STRING,
WOGBTR TYPE STRING,
WOGBTRO TYPE STRING,
BUKRS1 TYPE STRING,
GJAHR1 TYPE STRING,
BELNR1 TYPE STRING,
BUZEI1 TYPE STRING,
BUKRS2 TYPE STRING,
GJAHR2 TYPE STRING,
BELNR2 TYPE STRING,
BUZEI2 TYPE STRING,
EBELN TYPE STRING,
EBELP TYPE STRING,
ETENRM TYPE STRING,
STUNR TYPE STRING,
LIFNR TYPE STRING,
KUNNR TYPE STRING,
GSBER TYPE STRING,
BUKRS TYPE STRING,
EINDT TYPE STRING,
SGTXT TYPE STRING,
GEBER TYPE STRING,
KURSF TYPE STRING,
KUFIX TYPE STRING,
GBETR TYPE STRING,
KURSR TYPE STRING,
KURSFIKRS TYPE STRING,
KZKRS TYPE STRING,
UMBKZ TYPE STRING,
KBLNR TYPE STRING,
KBLPOS TYPE STRING,
HI_FIVOR TYPE STRING,
LO_FIVOR TYPE STRING,
HI_ACCOUNT TYPE STRING,
LO_ACCOUNT TYPE STRING,
HI_LIFNR TYPE STRING,
LO_LIFNR TYPE STRING,
HI_KUNNR TYPE STRING,
LO_KUNNR TYPE STRING,
XDELETE TYPE STRING,
HI_XOPVW TYPE STRING,
LO_XOPVW TYPE STRING,
CHAINBELNR TYPE STRING,
MWSKZ TYPE STRING,
LEDNR TYPE STRING,
BELTP TYPE STRING,
VERSN TYPE STRING,
OWAER TYPE STRING,
CPUDT TYPE STRING,
CPUTM TYPE STRING,
VBELN TYPE STRING,
POSNR TYPE STRING,
AUFPL TYPE STRING,
APLZL TYPE STRING,
RSNUM TYPE STRING,
RSPOS TYPE STRING,END OF T_EKKO_STR. DATA: WA_KCSCOFP_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_KCSCOFP_STR-MANDT sy-vline
WA_KCSCOFP_STR-KOKRS sy-vline
WA_KCSCOFP_STR-BELNR sy-vline
WA_KCSCOFP_STR-BUZEI sy-vline
WA_KCSCOFP_STR-TWAER sy-vline
WA_KCSCOFP_STR-GJAHR sy-vline
WA_KCSCOFP_STR-PERIO sy-vline
WA_KCSCOFP_STR-WRTTP sy-vline
WA_KCSCOFP_STR-VRGNG sy-vline
WA_KCSCOFP_STR-POSIT sy-vline
WA_KCSCOFP_STR-OBJNR sy-vline
WA_KCSCOFP_STR-HKONT sy-vline
WA_KCSCOFP_STR-WKGBTR sy-vline
WA_KCSCOFP_STR-WKGBTRO sy-vline
WA_KCSCOFP_STR-WTGBTR sy-vline
WA_KCSCOFP_STR-WTGBTRO sy-vline
WA_KCSCOFP_STR-WOGBTR sy-vline
WA_KCSCOFP_STR-WOGBTRO sy-vline
WA_KCSCOFP_STR-BUKRS1 sy-vline
WA_KCSCOFP_STR-GJAHR1 sy-vline
WA_KCSCOFP_STR-BELNR1 sy-vline
WA_KCSCOFP_STR-BUZEI1 sy-vline
WA_KCSCOFP_STR-BUKRS2 sy-vline
WA_KCSCOFP_STR-GJAHR2 sy-vline
WA_KCSCOFP_STR-BELNR2 sy-vline
WA_KCSCOFP_STR-BUZEI2 sy-vline
WA_KCSCOFP_STR-EBELN sy-vline
WA_KCSCOFP_STR-EBELP sy-vline
WA_KCSCOFP_STR-ETENRM sy-vline
WA_KCSCOFP_STR-STUNR sy-vline
WA_KCSCOFP_STR-LIFNR sy-vline
WA_KCSCOFP_STR-KUNNR sy-vline
WA_KCSCOFP_STR-GSBER sy-vline
WA_KCSCOFP_STR-BUKRS sy-vline
WA_KCSCOFP_STR-EINDT sy-vline
WA_KCSCOFP_STR-SGTXT sy-vline
WA_KCSCOFP_STR-GEBER sy-vline
WA_KCSCOFP_STR-KURSF sy-vline
WA_KCSCOFP_STR-KUFIX sy-vline
WA_KCSCOFP_STR-GBETR sy-vline
WA_KCSCOFP_STR-KURSR sy-vline
WA_KCSCOFP_STR-KURSFIKRS sy-vline
WA_KCSCOFP_STR-KZKRS sy-vline
WA_KCSCOFP_STR-UMBKZ sy-vline
WA_KCSCOFP_STR-KBLNR sy-vline
WA_KCSCOFP_STR-KBLPOS sy-vline
WA_KCSCOFP_STR-HI_FIVOR sy-vline
WA_KCSCOFP_STR-LO_FIVOR sy-vline
WA_KCSCOFP_STR-HI_ACCOUNT sy-vline
WA_KCSCOFP_STR-LO_ACCOUNT sy-vline
WA_KCSCOFP_STR-HI_LIFNR sy-vline
WA_KCSCOFP_STR-LO_LIFNR sy-vline
WA_KCSCOFP_STR-HI_KUNNR sy-vline
WA_KCSCOFP_STR-LO_KUNNR sy-vline
WA_KCSCOFP_STR-XDELETE sy-vline
WA_KCSCOFP_STR-HI_XOPVW sy-vline
WA_KCSCOFP_STR-LO_XOPVW sy-vline
WA_KCSCOFP_STR-CHAINBELNR sy-vline
WA_KCSCOFP_STR-MWSKZ sy-vline
WA_KCSCOFP_STR-LEDNR sy-vline
WA_KCSCOFP_STR-BELTP sy-vline
WA_KCSCOFP_STR-VERSN sy-vline
WA_KCSCOFP_STR-OWAER sy-vline
WA_KCSCOFP_STR-CPUDT sy-vline
WA_KCSCOFP_STR-CPUTM sy-vline
WA_KCSCOFP_STR-VBELN sy-vline
WA_KCSCOFP_STR-POSNR sy-vline
WA_KCSCOFP_STR-AUFPL sy-vline
WA_KCSCOFP_STR-APLZL sy-vline
WA_KCSCOFP_STR-RSNUM sy-vline
WA_KCSCOFP_STR-RSPOS sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.