ABAP Select data from SAP table BAPI_CPE_TERMOUT 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 BAPI_CPE_TERMOUT 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 BAPI_CPE_TERMOUT. 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 BAPI_CPE_TERMOUT 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_BAPI_CPE_TERMOUT TYPE STANDARD TABLE OF BAPI_CPE_TERMOUT,
      WA_BAPI_CPE_TERMOUT TYPE BAPI_CPE_TERMOUT,
      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: <BAPI_CPE_TERMOUT> TYPE BAPI_CPE_TERMOUT.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM BAPI_CPE_TERMOUT
*  INTO TABLE @DATA(IT_BAPI_CPE_TERMOUT2).
*--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_BAPI_CPE_TERMOUT INDEX 1 INTO DATA(WA_BAPI_CPE_TERMOUT2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_BAPI_CPE_TERMOUT ASSIGNING <BAPI_CPE_TERMOUT>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<BAPI_CPE_TERMOUT>-OPERATION = 1.
<BAPI_CPE_TERMOUT>-DOCITEM_GUID = 1.
<BAPI_CPE_TERMOUT>-COND_TYPE = 1.
<BAPI_CPE_TERMOUT>-TERMNO = 1.
<BAPI_CPE_TERMOUT>-TERMCONVIN_GUID = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_BAPI_CPE_TERMOUT-TERMROUNDIN_GUID, sy-vline,
WA_BAPI_CPE_TERMOUT-USEDQUAN, sy-vline,
WA_BAPI_CPE_TERMOUT-USEDUOM, sy-vline,
WA_BAPI_CPE_TERMOUT-TERMCONVOUT_GUID, sy-vline,
WA_BAPI_CPE_TERMOUT-PERDTOUT_GUID_Q, sy-vline,
WA_BAPI_CPE_TERMOUT-PERDTOUT_GUID_C, sy-vline.
ENDLOOP. *Add any further fields from structure WA_BAPI_CPE_TERMOUT 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_BAPI_CPE_TERMOUT 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_BAPI_CPE_TERMOUT INTO WA_BAPI_CPE_TERMOUT. *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 USEDUOM CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_BAPI_CPE_TERMOUT-USEDUOM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CPE_TERMOUT-USEDUOM.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit TSTPS, internal->external for field FORECAST_DATETIME CALL FUNCTION 'CONVERSION_EXIT_TSTPS_OUTPUT' EXPORTING input = WA_BAPI_CPE_TERMOUT-FORECAST_DATETIME IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CPE_TERMOUT-FORECAST_DATETIME.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit CUNIT, internal->external for field SURCHRATEUOM CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_BAPI_CPE_TERMOUT-SURCHRATEUOM IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_BAPI_CPE_TERMOUT-SURCHRATEUOM.
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_BAPI_CPE_TERMOUT_STR,
OPERATION TYPE STRING,
DOCITEM_GUID TYPE STRING,
COND_TYPE TYPE STRING,
TERMNO TYPE STRING,
TERMCONVIN_GUID TYPE STRING,
TERMROUNDIN_GUID TYPE STRING,
USEDQUAN TYPE STRING,
USEDUOM TYPE STRING,
TERMCONVOUT_GUID TYPE STRING,
PERDTOUT_GUID_Q TYPE STRING,
PERDTOUT_GUID_C TYPE STRING,
TERMSTATUS TYPE STRING,
BILLING_BLOCK TYPE STRING,
TERMVAL TYPE STRING,
TERMRATE TYPE STRING,
TERMCURR TYPE STRING,
TERMPRUN TYPE STRING,
TERMUOM TYPE STRING,
FORECAST_STATUS TYPE STRING,
FORECAST_DATETIME TYPE STRING,
CURVE_DATE TYPE STRING,
TERMRATE_DF34 TYPE STRING,
TERMVAL_DF34 TYPE STRING,
QUOTATION TYPE STRING,
QUOTATION_DF34 TYPE STRING,
QUOTCURR TYPE STRING,
QUOTPRUN TYPE STRING,
QUOTUOM TYPE STRING,
CALC_LIMITUSED TYPE STRING,
QUOT_UOMSUBST TYPE STRING,
SURCHARGERATE TYPE STRING,
SURCHARGERATE_DF34 TYPE STRING,
SURCHRATECURR TYPE STRING,
SURCHRATEPRUN TYPE STRING,
SURCHRATEUOM TYPE STRING,
SURCH_UOMSUBST TYPE STRING,
EXTENSION_GROUP TYPE STRING,
EXTENSION_USAGE TYPE STRING,
IS_BASIS_ID_MANUAL TYPE STRING,
BASIS_ID TYPE STRING,
FUT_KSCHL TYPE STRING,
FUT_TERMNO TYPE STRING,
FLAT_GRP_TIMESTAMP TYPE STRING,
USEDQUAN_ISO TYPE STRING,
USEDUOM_ISO TYPE STRING,
TERMVAL_ISO TYPE STRING,
TERMRATE_ISO TYPE STRING,
TERMCURR_ISO TYPE STRING,
TERMUOM_ISO TYPE STRING,
QUOTATION_ISO TYPE STRING,
QUOTCURR_ISO TYPE STRING,
QUOTUOM_ISO TYPE STRING,
SURCHRATE_ISO TYPE STRING,
SURCHCURR_ISO TYPE STRING,
SURCHUOM_ISO TYPE STRING,END OF T_EKKO_STR. DATA: WA_BAPI_CPE_TERMOUT_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_BAPI_CPE_TERMOUT_STR-OPERATION sy-vline
WA_BAPI_CPE_TERMOUT_STR-DOCITEM_GUID sy-vline
WA_BAPI_CPE_TERMOUT_STR-COND_TYPE sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMNO sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMCONVIN_GUID sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMROUNDIN_GUID sy-vline
WA_BAPI_CPE_TERMOUT_STR-USEDQUAN sy-vline
WA_BAPI_CPE_TERMOUT_STR-USEDUOM sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMCONVOUT_GUID sy-vline
WA_BAPI_CPE_TERMOUT_STR-PERDTOUT_GUID_Q sy-vline
WA_BAPI_CPE_TERMOUT_STR-PERDTOUT_GUID_C sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMSTATUS sy-vline
WA_BAPI_CPE_TERMOUT_STR-BILLING_BLOCK sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMVAL sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMRATE sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMCURR sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMPRUN sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMUOM sy-vline
WA_BAPI_CPE_TERMOUT_STR-FORECAST_STATUS sy-vline
WA_BAPI_CPE_TERMOUT_STR-FORECAST_DATETIME sy-vline
WA_BAPI_CPE_TERMOUT_STR-CURVE_DATE sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMRATE_DF34 sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMVAL_DF34 sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTATION sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTATION_DF34 sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTCURR sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTPRUN sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTUOM sy-vline
WA_BAPI_CPE_TERMOUT_STR-CALC_LIMITUSED sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOT_UOMSUBST sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHARGERATE sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHARGERATE_DF34 sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHRATECURR sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHRATEPRUN sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHRATEUOM sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCH_UOMSUBST sy-vline
WA_BAPI_CPE_TERMOUT_STR-EXTENSION_GROUP sy-vline
WA_BAPI_CPE_TERMOUT_STR-EXTENSION_USAGE sy-vline
WA_BAPI_CPE_TERMOUT_STR-IS_BASIS_ID_MANUAL sy-vline
WA_BAPI_CPE_TERMOUT_STR-BASIS_ID sy-vline
WA_BAPI_CPE_TERMOUT_STR-FUT_KSCHL sy-vline
WA_BAPI_CPE_TERMOUT_STR-FUT_TERMNO sy-vline
WA_BAPI_CPE_TERMOUT_STR-FLAT_GRP_TIMESTAMP sy-vline
WA_BAPI_CPE_TERMOUT_STR-USEDQUAN_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-USEDUOM_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMVAL_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMRATE_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMCURR_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-TERMUOM_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTATION_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTCURR_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-QUOTUOM_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHRATE_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHCURR_ISO sy-vline
WA_BAPI_CPE_TERMOUT_STR-SURCHUOM_ISO sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.