ABAP Select data from SAP table KCSLAB 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 KCSLAB 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 KCSLAB. 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 KCSLAB 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_KCSLAB TYPE STANDARD TABLE OF KCSLAB,
      WA_KCSLAB TYPE KCSLAB,
      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: <KCSLAB> TYPE KCSLAB.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM KCSLAB
*  INTO TABLE @DATA(IT_KCSLAB2).
*--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_KCSLAB INDEX 1 INTO DATA(WA_KCSLAB2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_KCSLAB ASSIGNING <KCSLAB>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<KCSLAB>-MANDT = 1.
<KCSLAB>-KOKRS = 1.
<KCSLAB>-LSTAR = 1.
<KCSLAB>-DATBI = 1.
<KCSLAB>-DATAB = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_KCSLAB-ERSDA, sy-vline,
WA_KCSLAB-USNAM, sy-vline,
WA_KCSLAB-LEINH, sy-vline,
WA_KCSLAB-KSTTY, sy-vline,
WA_KCSLAB-AUSEH, sy-vline,
WA_KCSLAB-AUSFK, sy-vline.
ENDLOOP. *Add any further fields from structure WA_KCSLAB 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_KCSLAB 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_KCSLAB INTO WA_KCSLAB. *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 CUNIT, internal->external for field LEINH CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_KCSLAB-LEINH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCSLAB-LEINH.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field AUSEH CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_KCSLAB-AUSEH IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCSLAB-AUSEH.
WRITE:/ 'New Value:', ld_input.

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

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

*Conversion exit ALPHA, internal->external for field TARKZ_I CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_KCSLAB-TARKZ_I IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_KCSLAB-TARKZ_I.
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_KCSLAB_STR,
MANDT TYPE STRING,
KOKRS TYPE STRING,
LSTAR TYPE STRING,
DATBI TYPE STRING,
DATAB TYPE STRING,
ERSDA TYPE STRING,
USNAM TYPE STRING,
LEINH TYPE STRING,
KSTTY TYPE STRING,
AUSEH TYPE STRING,
AUSFK TYPE STRING,
VKSTA TYPE STRING,
LATYP TYPE STRING,
LARK1 TYPE STRING,
LARK2 TYPE STRING,
SPRKZ TYPE STRING,
FIXVO TYPE STRING,
TARKZ TYPE STRING,
YRATE TYPE STRING,
TARKZ_I TYPE STRING,
LATYPI TYPE STRING,
MANIST TYPE STRING,
KTEXT TYPE STRING,
LTEXT TYPE STRING,
MANPLAN TYPE STRING,
KZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_KCSLAB_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_KCSLAB_STR-MANDT sy-vline
WA_KCSLAB_STR-KOKRS sy-vline
WA_KCSLAB_STR-LSTAR sy-vline
WA_KCSLAB_STR-DATBI sy-vline
WA_KCSLAB_STR-DATAB sy-vline
WA_KCSLAB_STR-ERSDA sy-vline
WA_KCSLAB_STR-USNAM sy-vline
WA_KCSLAB_STR-LEINH sy-vline
WA_KCSLAB_STR-KSTTY sy-vline
WA_KCSLAB_STR-AUSEH sy-vline
WA_KCSLAB_STR-AUSFK sy-vline
WA_KCSLAB_STR-VKSTA sy-vline
WA_KCSLAB_STR-LATYP sy-vline
WA_KCSLAB_STR-LARK1 sy-vline
WA_KCSLAB_STR-LARK2 sy-vline
WA_KCSLAB_STR-SPRKZ sy-vline
WA_KCSLAB_STR-FIXVO sy-vline
WA_KCSLAB_STR-TARKZ sy-vline
WA_KCSLAB_STR-YRATE sy-vline
WA_KCSLAB_STR-TARKZ_I sy-vline
WA_KCSLAB_STR-LATYPI sy-vline
WA_KCSLAB_STR-MANIST sy-vline
WA_KCSLAB_STR-KTEXT sy-vline
WA_KCSLAB_STR-LTEXT sy-vline
WA_KCSLAB_STR-MANPLAN sy-vline
WA_KCSLAB_STR-KZ sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.