ABAP Select data from SAP table K002 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 K002 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 K002. 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 K002 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_K002 TYPE STANDARD TABLE OF K002,
      WA_K002 TYPE K002,
      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: <K002> TYPE K002.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM K002
*  INTO TABLE @DATA(IT_K0022).
*--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_K002 INDEX 1 INTO DATA(WA_K0022).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_K002 ASSIGNING <K002>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<K002>-MANDT = 1.
<K002>-KSCHL = 1.
<K002>-VKORG = 1.
<K002>-VTWEG = 1.
<K002>-KUNNR = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_K002-DATBI, sy-vline,
WA_K002-DATAB, sy-vline,
WA_K002-KNUMH, sy-vline,
ENDLOOP. *Add any further fields from structure WA_K002 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_K002 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_K002 INTO WA_K002. *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 KUNNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_K002-KUNNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_K002-KUNNR.
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_K002_STR,
MANDT TYPE STRING,
KSCHL TYPE STRING,
VKORG TYPE STRING,
VTWEG TYPE STRING,
KUNNR TYPE STRING,
DATBI TYPE STRING,
DATAB TYPE STRING,
KNUMH TYPE STRING,END OF T_EKKO_STR. DATA: WA_K002_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_K002_STR-MANDT sy-vline
WA_K002_STR-KSCHL sy-vline
WA_K002_STR-VKORG sy-vline
WA_K002_STR-VTWEG sy-vline
WA_K002_STR-KUNNR sy-vline
WA_K002_STR-DATBI sy-vline
WA_K002_STR-DATAB sy-vline
WA_K002_STR-KNUMH sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.