ABAP Select data from SAP table BAPIINCLCOSTESTITEMEXPLOSION 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 BAPIINCLCOSTESTITEMEXPLOSION 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 BAPIINCLCOSTESTITEMEXPLOSION. 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 BAPIINCLCOSTESTITEMEXPLOSION 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_BAPIINCLCOSTESTITEMEXPLOSION TYPE STANDARD TABLE OF BAPIINCLCOSTESTITEMEXPLOSION,
      WA_BAPIINCLCOSTESTITEMEXPLOSION TYPE BAPIINCLCOSTESTITEMEXPLOSION,
      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: <BAPIINCLCOSTESTITEMEXPLOSION> TYPE BAPIINCLCOSTESTITEMEXPLOSION.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPIINCLCOSTESTITEMEXPLOSION
*  INTO TABLE @DATA(IT_BAPIINCLCOSTESTITEMEXPLOSION2).
*--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_BAPIINCLCOSTESTITEMEXPLOSION INDEX 1 INTO DATA(WA_BAPIINCLCOSTESTITEMEXPLOSION2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPIINCLCOSTESTITEMEXPLOSION ASSIGNING <BAPIINCLCOSTESTITEMEXPLOSION>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPIINCLCOSTESTITEMEXPLOSION>-MATERIAL = 1.
<BAPIINCLCOSTESTITEMEXPLOSION>-PLANT = 1.
<BAPIINCLCOSTESTITEMEXPLOSION>-ITEM_CATEGORY = 1.
<BAPIINCLCOSTESTITEMEXPLOSION>-COST_ELEMENT = 1.
<BAPIINCLCOSTESTITEMEXPLOSION>-ORIGIN_GROUP = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPIINCLCOSTESTITEMEXPLOSION-COST_COMPONENT, sy-vline,
WA_BAPIINCLCOSTESTITEMEXPLOSION-PRICE_QUANTITY_UNIT, sy-vline,
WA_BAPIINCLCOSTESTITEMEXPLOSION-CO_AREA_CURRENCY_KEY, sy-vline,
WA_BAPIINCLCOSTESTITEMEXPLOSION-CO_AREA_CURRENCY_PRICE_UNIT, sy-vline,
WA_BAPIINCLCOSTESTITEMEXPLOSION-CO_AREA_CURRENCY_TOTAL_PRICE, sy-vline,
WA_BAPIINCLCOSTESTITEMEXPLOSION-CO_AREA_CURRENCY_FIXED_PRICE, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPIINCLCOSTESTITEMEXPLOSION 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_BAPIINCLCOSTESTITEMEXPLOSION 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_BAPIINCLCOSTESTITEMEXPLOSION INTO WA_BAPIINCLCOSTESTITEMEXPLOSION. *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 MATN5, internal->external for field MATERIAL CALL FUNCTION 'CONVERSION_EXIT_MATN5_OUTPUT' EXPORTING input = WA_BAPIINCLCOSTESTITEMEXPLOSION-MATERIAL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPIINCLCOSTESTITEMEXPLOSION-MATERIAL.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

*Conversion exit NUMCV, internal->external for field OPERATION CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_BAPIINCLCOSTESTITEMEXPLOSION-OPERATION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPIINCLCOSTESTITEMEXPLOSION-OPERATION.
WRITE:/ 'New Value:', ld_input.

*Conversion exit NUMCV, internal->external for field SUB_OPERATION CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_BAPIINCLCOSTESTITEMEXPLOSION-SUB_OPERATION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPIINCLCOSTESTITEMEXPLOSION-SUB_OPERATION.
WRITE:/ 'New Value:', ld_input.

*Conversion exit NUMCV, internal->external for field BOM_ITEM_NUMBER CALL FUNCTION 'CONVERSION_EXIT_NUMCV_OUTPUT' EXPORTING input = WA_BAPIINCLCOSTESTITEMEXPLOSION-BOM_ITEM_NUMBER IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPIINCLCOSTESTITEMEXPLOSION-BOM_ITEM_NUMBER.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ALPHA, internal->external for field PERCHASING_INFORMATION_RECORD CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_BAPIINCLCOSTESTITEMEXPLOSION-PERCHASING_INFORMATION_RECORD IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPIINCLCOSTESTITEMEXPLOSION-PERCHASING_INFORMATION_RECORD.
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_BAPIINCLCOSTESTITEMEXPLOSION_STR,
MATERIAL TYPE STRING,
PLANT TYPE STRING,
ITEM_CATEGORY TYPE STRING,
COST_ELEMENT TYPE STRING,
ORIGIN_GROUP TYPE STRING,
COST_COMPONENT TYPE STRING,
PRICE_QUANTITY_UNIT TYPE STRING,
CO_AREA_CURRENCY_KEY TYPE STRING,
CO_AREA_CURRENCY_PRICE_UNIT TYPE STRING,
CO_AREA_CURRENCY_TOTAL_PRICE TYPE STRING,
CO_AREA_CURRENCY_FIXED_PRICE TYPE STRING,
CO_AREA_CURRENCY_TOTAL_VALUE TYPE STRING,
CO_AREA_CURRENCY_FIXED_VALUE TYPE STRING,
CCODE_CURRENCY_KEY TYPE STRING,
CCODE_CURRENCY_PRICE_UNIT TYPE STRING,
CCODE_CURRENCY_TOTAL_PRICE TYPE STRING,
CCODE_CURRENCY_FIXED_PRICE TYPE STRING,
CCODE_CURRENCY_TOTAL_VALUE TYPE STRING,
CCODE_CURRENCY_FIXED_VALUE TYPE STRING,
QUANTITY TYPE STRING,
BASE_UNIT_OF_MEASURE TYPE STRING,
ASSEMBLY_SCRAP_QUANTITY TYPE STRING,
COMPONENT_SCRAP_QUANTITY TYPE STRING,
COST_CENTER TYPE STRING,
ACTIVITY_TYPE TYPE STRING,
WORK_CENTER TYPE STRING,
WORK_CENTER_PLANT TYPE STRING,
OPERATION TYPE STRING,
SUB_OPERATION TYPE STRING,
BOM_ITEM_NUMBER TYPE STRING,
PROCESS_NUMBER TYPE STRING,
PERCHASING_INFORMATION_RECORD TYPE STRING,
PERCHASING_ORGANIZATION TYPE STRING,
DESCRIPTION TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPIINCLCOSTESTITEMEXPLOSION_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_BAPIINCLCOSTESTITEMEXPLOSION_STR-MATERIAL sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-PLANT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-ITEM_CATEGORY sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-COST_ELEMENT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-ORIGIN_GROUP sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-COST_COMPONENT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-PRICE_QUANTITY_UNIT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CO_AREA_CURRENCY_KEY sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CO_AREA_CURRENCY_PRICE_UNIT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CO_AREA_CURRENCY_TOTAL_PRICE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CO_AREA_CURRENCY_FIXED_PRICE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CO_AREA_CURRENCY_TOTAL_VALUE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CO_AREA_CURRENCY_FIXED_VALUE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CCODE_CURRENCY_KEY sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CCODE_CURRENCY_PRICE_UNIT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CCODE_CURRENCY_TOTAL_PRICE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CCODE_CURRENCY_FIXED_PRICE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CCODE_CURRENCY_TOTAL_VALUE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-CCODE_CURRENCY_FIXED_VALUE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-QUANTITY sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-BASE_UNIT_OF_MEASURE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-ASSEMBLY_SCRAP_QUANTITY sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-COMPONENT_SCRAP_QUANTITY sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-COST_CENTER sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-ACTIVITY_TYPE sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-WORK_CENTER sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-WORK_CENTER_PLANT sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-OPERATION sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-SUB_OPERATION sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-BOM_ITEM_NUMBER sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-PROCESS_NUMBER sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-PERCHASING_INFORMATION_RECORD sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-PERCHASING_ORGANIZATION sy-vline
WA_BAPIINCLCOSTESTITEMEXPLOSION_STR-DESCRIPTION sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.