ABAP Select data from SAP table CACS_S_KOND_TAB_LAST 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 CACS_S_KOND_TAB_LAST 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 CACS_S_KOND_TAB_LAST. 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 CACS_S_KOND_TAB_LAST 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_CACS_S_KOND_TAB_LAST TYPE STANDARD TABLE OF CACS_S_KOND_TAB_LAST,
      WA_CACS_S_KOND_TAB_LAST TYPE CACS_S_KOND_TAB_LAST,
      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: <CACS_S_KOND_TAB_LAST> TYPE CACS_S_KOND_TAB_LAST.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM CACS_S_KOND_TAB_LAST
*  INTO TABLE @DATA(IT_CACS_S_KOND_TAB_LAST2).
*--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_CACS_S_KOND_TAB_LAST INDEX 1 INTO DATA(WA_CACS_S_KOND_TAB_LAST2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_CACS_S_KOND_TAB_LAST ASSIGNING <CACS_S_KOND_TAB_LAST>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<CACS_S_KOND_TAB_LAST>-MANDT = 1.
<CACS_S_KOND_TAB_LAST>-KAPPL = 1.
<CACS_S_KOND_TAB_LAST>-KSCHL = 1.
<CACS_S_KOND_TAB_LAST>-VAKEY = 1.
<CACS_S_KOND_TAB_LAST>-DATBI = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_CACS_S_KOND_TAB_LAST-DATAB, sy-vline,
WA_CACS_S_KOND_TAB_LAST-VADAT, sy-vline,
WA_CACS_S_KOND_TAB_LAST-KNUMH, sy-vline,
WA_CACS_S_KOND_TAB_LAST-P_STATE, sy-vline,
WA_CACS_S_KOND_TAB_LAST-P_VERSION, sy-vline,
WA_CACS_S_KOND_TAB_LAST-P_CACSAPPL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_CACS_S_KOND_TAB_LAST 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_CACS_S_KOND_TAB_LAST 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_CACS_S_KOND_TAB_LAST INTO WA_CACS_S_KOND_TAB_LAST. *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 TSTPS, internal->external for field P_VERSION CALL FUNCTION 'CONVERSION_EXIT_TSTPS_OUTPUT' EXPORTING input = WA_CACS_S_KOND_TAB_LAST-P_VERSION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CACS_S_KOND_TAB_LAST-P_VERSION.
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_CACS_S_KOND_TAB_LAST_STR,
MANDT TYPE STRING,
KAPPL TYPE STRING,
KSCHL TYPE STRING,
VAKEY TYPE STRING,
DATBI TYPE STRING,
DATAB TYPE STRING,
VADAT TYPE STRING,
KNUMH TYPE STRING,
P_STATE TYPE STRING,
P_VERSION TYPE STRING,
P_CACSAPPL TYPE STRING,
P_NUM1 TYPE STRING,
P_NUM2 TYPE STRING,
P_AMNT1 TYPE STRING,
P_AMNT2 TYPE STRING,
P_CURR1 TYPE STRING,
P_CURR2 TYPE STRING,
P_TEXT1 TYPE STRING,
P_TEXT2 TYPE STRING,
P_NUM1_STD TYPE STRING,
P_NUM2_STD TYPE STRING,
P_AMNT1_STD TYPE STRING,
P_AMNT2_STD TYPE STRING,
P_CURR1_STD TYPE STRING,
P_CURR2_STD TYPE STRING,
P_TEXT1_STD TYPE STRING,
P_TEXT2_STD TYPE STRING,
KNUMH_STD TYPE STRING,
KBETR_STD TYPE STRING,
KONWA_STD TYPE STRING,
DATBI_STD TYPE STRING,
DATAB_STD TYPE STRING,
KONWA TYPE STRING,
KBETR TYPE STRING,
P_STATE_OLD TYPE STRING,
LINE_NUMBER TYPE STRING,
VTEXT TYPE STRING,
INDEX_STANDARD TYPE STRING,END OF T_EKKO_STR. DATA: WA_CACS_S_KOND_TAB_LAST_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_CACS_S_KOND_TAB_LAST_STR-MANDT sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KAPPL sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KSCHL sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-VAKEY sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-DATBI sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-DATAB sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-VADAT sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KNUMH sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_STATE sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_VERSION sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_CACSAPPL sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_NUM1 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_NUM2 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_AMNT1 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_AMNT2 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_CURR1 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_CURR2 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_TEXT1 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_TEXT2 sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_NUM1_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_NUM2_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_AMNT1_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_AMNT2_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_CURR1_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_CURR2_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_TEXT1_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_TEXT2_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KNUMH_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KBETR_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KONWA_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-DATBI_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-DATAB_STD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KONWA sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-KBETR sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-P_STATE_OLD sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-LINE_NUMBER sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-VTEXT sy-vline
WA_CACS_S_KOND_TAB_LAST_STR-INDEX_STANDARD sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.