ABAP Select data from SAP table FKKOP_NC_ANZANFO 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 FKKOP_NC_ANZANFO 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 FKKOP_NC_ANZANFO. 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 FKKOP_NC_ANZANFO 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_FKKOP_NC_ANZANFO TYPE STANDARD TABLE OF FKKOP_NC_ANZANFO,
      WA_FKKOP_NC_ANZANFO TYPE FKKOP_NC_ANZANFO,
      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: <FKKOP_NC_ANZANFO> TYPE FKKOP_NC_ANZANFO.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM FKKOP_NC_ANZANFO
*  INTO TABLE @DATA(IT_FKKOP_NC_ANZANFO2).
*--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_FKKOP_NC_ANZANFO INDEX 1 INTO DATA(WA_FKKOP_NC_ANZANFO2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_FKKOP_NC_ANZANFO ASSIGNING <FKKOP_NC_ANZANFO>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<FKKOP_NC_ANZANFO>-BUKRS = 1.
<FKKOP_NC_ANZANFO>-GSBER = 1.
<FKKOP_NC_ANZANFO>-GPART = 1.
<FKKOP_NC_ANZANFO>-VTREF = 1.
<FKKOP_NC_ANZANFO>-VKONT = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_FKKOP_NC_ANZANFO-ABWBL, sy-vline,
WA_FKKOP_NC_ANZANFO-ABWTP, sy-vline,
WA_FKKOP_NC_ANZANFO-ABWKT, sy-vline,
WA_FKKOP_NC_ANZANFO-TVORG, sy-vline,
WA_FKKOP_NC_ANZANFO-KOFIZ, sy-vline,
WA_FKKOP_NC_ANZANFO-SPART, sy-vline.
ENDLOOP. *Add any further fields from structure WA_FKKOP_NC_ANZANFO 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_FKKOP_NC_ANZANFO 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_FKKOP_NC_ANZANFO INTO WA_FKKOP_NC_ANZANFO. *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 GPART CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FKKOP_NC_ANZANFO-GPART IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FKKOP_NC_ANZANFO-GPART.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field VBUND CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FKKOP_NC_ANZANFO-VBUND IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FKKOP_NC_ANZANFO-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_FKKOP_NC_ANZANFO_STR,
BUKRS TYPE STRING,
GSBER TYPE STRING,
GPART TYPE STRING,
VTREF TYPE STRING,
VKONT TYPE STRING,
ABWBL TYPE STRING,
ABWTP TYPE STRING,
ABWKT TYPE STRING,
TVORG TYPE STRING,
KOFIZ TYPE STRING,
SPART TYPE STRING,
HKONT TYPE STRING,
MWSKZ TYPE STRING,
XANZA TYPE STRING,
STAKZ TYPE STRING,
ASTKZ TYPE STRING,
BUDAT TYPE STRING,
WAERS TYPE STRING,
KURSF TYPE STRING,
TXJCD TYPE STRING,
TXDAT TYPE STRING,
MWSKO TYPE STRING,
MWVKO TYPE STRING,
VBUND TYPE STRING,
BUPLA TYPE STRING,
BLART TYPE STRING,
QSSKZ TYPE STRING,
QSPTP TYPE STRING,END OF T_EKKO_STR. DATA: WA_FKKOP_NC_ANZANFO_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_FKKOP_NC_ANZANFO_STR-BUKRS sy-vline
WA_FKKOP_NC_ANZANFO_STR-GSBER sy-vline
WA_FKKOP_NC_ANZANFO_STR-GPART sy-vline
WA_FKKOP_NC_ANZANFO_STR-VTREF sy-vline
WA_FKKOP_NC_ANZANFO_STR-VKONT sy-vline
WA_FKKOP_NC_ANZANFO_STR-ABWBL sy-vline
WA_FKKOP_NC_ANZANFO_STR-ABWTP sy-vline
WA_FKKOP_NC_ANZANFO_STR-ABWKT sy-vline
WA_FKKOP_NC_ANZANFO_STR-TVORG sy-vline
WA_FKKOP_NC_ANZANFO_STR-KOFIZ sy-vline
WA_FKKOP_NC_ANZANFO_STR-SPART sy-vline
WA_FKKOP_NC_ANZANFO_STR-HKONT sy-vline
WA_FKKOP_NC_ANZANFO_STR-MWSKZ sy-vline
WA_FKKOP_NC_ANZANFO_STR-XANZA sy-vline
WA_FKKOP_NC_ANZANFO_STR-STAKZ sy-vline
WA_FKKOP_NC_ANZANFO_STR-ASTKZ sy-vline
WA_FKKOP_NC_ANZANFO_STR-BUDAT sy-vline
WA_FKKOP_NC_ANZANFO_STR-WAERS sy-vline
WA_FKKOP_NC_ANZANFO_STR-KURSF sy-vline
WA_FKKOP_NC_ANZANFO_STR-TXJCD sy-vline
WA_FKKOP_NC_ANZANFO_STR-TXDAT sy-vline
WA_FKKOP_NC_ANZANFO_STR-MWSKO sy-vline
WA_FKKOP_NC_ANZANFO_STR-MWVKO sy-vline
WA_FKKOP_NC_ANZANFO_STR-VBUND sy-vline
WA_FKKOP_NC_ANZANFO_STR-BUPLA sy-vline
WA_FKKOP_NC_ANZANFO_STR-BLART sy-vline
WA_FKKOP_NC_ANZANFO_STR-QSSKZ sy-vline
WA_FKKOP_NC_ANZANFO_STR-QSPTP sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.