ABAP Select data from SAP table OPP_DISCOUNT_BASE 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 OPP_DISCOUNT_BASE 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 OPP_DISCOUNT_BASE. 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 OPP_DISCOUNT_BASE 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_OPP_DISCOUNT_BASE TYPE STANDARD TABLE OF OPP_DISCOUNT_BASE,
      WA_OPP_DISCOUNT_BASE TYPE OPP_DISCOUNT_BASE,
      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: <OPP_DISCOUNT_BASE> TYPE OPP_DISCOUNT_BASE.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM OPP_DISCOUNT_BASE
*  INTO TABLE @DATA(IT_OPP_DISCOUNT_BASE2).
*--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_OPP_DISCOUNT_BASE INDEX 1 INTO DATA(WA_OPP_DISCOUNT_BASE2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_OPP_DISCOUNT_BASE ASSIGNING <OPP_DISCOUNT_BASE>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<OPP_DISCOUNT_BASE>-CONTROLLER = 1.
<OPP_DISCOUNT_BASE>-BASE = 1.
<OPP_DISCOUNT_BASE>-CONTROLLER = 1.
<OPP_DISCOUNT_BASE>-BASE = 1.
<OPP_DISCOUNT_BASE>-CONTROLLER = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_OPP_DISCOUNT_BASE-SEQUENCE_NUMBER, sy-vline,
WA_OPP_DISCOUNT_BASE-AMOUNT, sy-vline,
WA_OPP_DISCOUNT_BASE-CONTROLLER, sy-vline,
WA_OPP_DISCOUNT_BASE-BASE, sy-vline,
WA_OPP_DISCOUNT_BASE-CONTROLLER, sy-vline,
WA_OPP_DISCOUNT_BASE-CURRENCY, sy-vline.
ENDLOOP. *Add any further fields from structure WA_OPP_DISCOUNT_BASE 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_OPP_DISCOUNT_BASE 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_OPP_DISCOUNT_BASE INTO WA_OPP_DISCOUNT_BASE. *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.
ENDFORM. *&---------------------------------------------------------------------* *& Form process_as_string_field_values *&---------------------------------------------------------------------* FORM process_as_string_field_values CHANGING p_EKKO LIKE wa_EKKO. TYPES: BEGIN OF T_OPP_DISCOUNT_BASE_STR,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
SEQUENCE_NUMBER TYPE STRING,
AMOUNT TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
CURRENCY TYPE STRING,
CONTENT TYPE STRING,
ACTION TYPE STRING,
PERCENT TYPE STRING,
CONTROLLER TYPE STRING,
ACTION TYPE STRING,
CONTENT TYPE STRING,
PREVIOUS_PRICE TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
CURRENCY TYPE STRING,
CONTENT TYPE STRING,
NEW_PRICE TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
CURRENCY TYPE STRING,
CONTENT TYPE STRING,
PROMOTION_ID TYPE STRING,
ITEM_LINK TYPE STRING,
QUANTITY TYPE STRING,
CONTROLLER TYPE STRING,
UNITS TYPE STRING,
UNIT_OF_MEASURE_CODE TYPE STRING,
CONTENT TYPE STRING,
ROUNDING TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
CURRENCY TYPE STRING,
CONTENT TYPE STRING,
ROUNDING_DIRECTION TYPE STRING,
COMPUTATION_BASE_AMOUNT TYPE STRING,
CONTROLLER TYPE STRING,
CURRENCY TYPE STRING,
CONTENT TYPE STRING,
PRICE_DERIVATION_RULE TYPE STRING,
ANY TYPE STRING,
MANUAL_TRIGGER_SEQUENCE_NUMBER TYPE STRING,
EXTRA_AMOUNT TYPE STRING,
CONTROLLER TYPE STRING,
CURRENCY TYPE STRING,
CONTENT TYPE STRING,
EXTERNAL_SYSTEM_ORIGINATOR_FLA TYPE STRING,
PRORATED_FLAG TYPE STRING,END OF T_EKKO_STR. DATA: WA_OPP_DISCOUNT_BASE_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_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-SEQUENCE_NUMBER sy-vline
WA_OPP_DISCOUNT_BASE_STR-AMOUNT sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-CURRENCY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-ACTION sy-vline
WA_OPP_DISCOUNT_BASE_STR-PERCENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-ACTION sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-PREVIOUS_PRICE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-CURRENCY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-NEW_PRICE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-CURRENCY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-PROMOTION_ID sy-vline
WA_OPP_DISCOUNT_BASE_STR-ITEM_LINK sy-vline
WA_OPP_DISCOUNT_BASE_STR-QUANTITY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-UNITS sy-vline
WA_OPP_DISCOUNT_BASE_STR-UNIT_OF_MEASURE_CODE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-ROUNDING sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-BASE sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-CURRENCY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-ROUNDING_DIRECTION sy-vline
WA_OPP_DISCOUNT_BASE_STR-COMPUTATION_BASE_AMOUNT sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-CURRENCY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-PRICE_DERIVATION_RULE sy-vline
WA_OPP_DISCOUNT_BASE_STR-ANY sy-vline
WA_OPP_DISCOUNT_BASE_STR-MANUAL_TRIGGER_SEQUENCE_NUMBER sy-vline
WA_OPP_DISCOUNT_BASE_STR-EXTRA_AMOUNT sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTROLLER sy-vline
WA_OPP_DISCOUNT_BASE_STR-CURRENCY sy-vline
WA_OPP_DISCOUNT_BASE_STR-CONTENT sy-vline
WA_OPP_DISCOUNT_BASE_STR-EXTERNAL_SYSTEM_ORIGINATOR_FLA sy-vline
WA_OPP_DISCOUNT_BASE_STR-PRORATED_FLAG sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.