ABAP Select data from SAP table CCRCOMPONENT 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 CCRCOMPONENT 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 CCRCOMPONENT. 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 CCRCOMPONENT 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_CCRCOMPONENT TYPE STANDARD TABLE OF CCRCOMPONENT,
      WA_CCRCOMPONENT TYPE CCRCOMPONENT,
      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: <CCRCOMPONENT> TYPE CCRCOMPONENT.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM CCRCOMPONENT
*  INTO TABLE @DATA(IT_CCRCOMPONENT2).
*--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_CCRCOMPONENT INDEX 1 INTO DATA(WA_CCRCOMPONENT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_CCRCOMPONENT ASSIGNING <CCRCOMPONENT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<CCRCOMPONENT>-MANDT = 1.
<CCRCOMPONENT>-CHMLCOMPUUID = 1.
<CCRCOMPONENT>-ACTIVECHMLCOMPUUID = 1.
<CCRCOMPONENT>-CHMLCOMPOSITIONUUID = 1.
<CCRCOMPONENT>-CHMLCMPLNCINFOUUID = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_CCRCOMPONENT-SUBSTANCEUUID, sy-vline,
WA_CCRCOMPONENT-COMPANYSUBSTANCENAVGNLINK, sy-vline,
WA_CCRCOMPONENT-SUBSTANCENAME, sy-vline,
WA_CCRCOMPONENT-CASNUMBER, sy-vline,
WA_CCRCOMPONENT-CHMLCOMPOSITIONSTATUS, sy-vline,
WA_CCRCOMPONENT-CHMLCOMPTYPE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_CCRCOMPONENT 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_CCRCOMPONENT 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_CCRCOMPONENT INTO WA_CCRCOMPONENT. *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 CHMLCOMPQTYUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_CCRCOMPONENT-CHMLCOMPQTYUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CCRCOMPONENT-CHMLCOMPQTYUNIT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit CUNIT, internal->external for field CHMLCOMPRELDQTYUNIT CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_CCRCOMPONENT-CHMLCOMPRELDQTYUNIT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_CCRCOMPONENT-CHMLCOMPRELDQTYUNIT.
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_CCRCOMPONENT_STR,
MANDT TYPE STRING,
CHMLCOMPUUID TYPE STRING,
ACTIVECHMLCOMPUUID TYPE STRING,
CHMLCOMPOSITIONUUID TYPE STRING,
CHMLCMPLNCINFOUUID TYPE STRING,
SUBSTANCEUUID TYPE STRING,
COMPANYSUBSTANCENAVGNLINK TYPE STRING,
SUBSTANCENAME TYPE STRING,
CASNUMBER TYPE STRING,
CHMLCOMPOSITIONSTATUS TYPE STRING,
CHMLCOMPTYPE TYPE STRING,
CHMLCOMPTYPENAME TYPE STRING,
CHMLCOMPQTY TYPE STRING,
CHMLCOMPQTYCRITLTY TYPE STRING,
CHMLCOMPQTYLOWERLIMIT TYPE STRING,
CHMLCOMPQTYLOWERLIMITCRITLTY TYPE STRING,
CHMLCOMPQTYUPPERLIMIT TYPE STRING,
CHMLCOMPQTYUPPERLIMITCRITLTY TYPE STRING,
CHMLCOMPQTYUNIT TYPE STRING,
CHMLCOMPQTYUNITCRITLTY TYPE STRING,
CHMLCOMPHASDIFFCRITLTY TYPE STRING,
CHMLCOMPHASDIFF TYPE STRING,
CHMLCOMPHASDIFFNAME TYPE STRING,
NMBROFSUBSTPRVDDBYSUPLRWTHTOT TYPE STRING,
CHMLCOMPRELDQTY TYPE STRING,
CHMLCOMPRELDLOWERLIMITQTY TYPE STRING,
CHMLCOMPRELDUPPERLIMITQTY TYPE STRING,
CHMLCOMPRELDQTYUNIT TYPE STRING,END OF T_EKKO_STR. DATA: WA_CCRCOMPONENT_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_CCRCOMPONENT_STR-MANDT sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPUUID sy-vline
WA_CCRCOMPONENT_STR-ACTIVECHMLCOMPUUID sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPOSITIONUUID sy-vline
WA_CCRCOMPONENT_STR-CHMLCMPLNCINFOUUID sy-vline
WA_CCRCOMPONENT_STR-SUBSTANCEUUID sy-vline
WA_CCRCOMPONENT_STR-COMPANYSUBSTANCENAVGNLINK sy-vline
WA_CCRCOMPONENT_STR-SUBSTANCENAME sy-vline
WA_CCRCOMPONENT_STR-CASNUMBER sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPOSITIONSTATUS sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPTYPE sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPTYPENAME sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYCRITLTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYLOWERLIMIT sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYLOWERLIMITCRITLTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYUPPERLIMIT sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYUPPERLIMITCRITLTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYUNIT sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPQTYUNITCRITLTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPHASDIFFCRITLTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPHASDIFF sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPHASDIFFNAME sy-vline
WA_CCRCOMPONENT_STR-NMBROFSUBSTPRVDDBYSUPLRWTHTOT sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPRELDQTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPRELDLOWERLIMITQTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPRELDUPPERLIMITQTY sy-vline
WA_CCRCOMPONENT_STR-CHMLCOMPRELDQTYUNIT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.