ABAP Select data from SAP table AMI_CAR_TYPE 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 AMI_CAR_TYPE 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 AMI_CAR_TYPE. 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 AMI_CAR_TYPE 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_AMI_CAR_TYPE TYPE STANDARD TABLE OF AMI_CAR_TYPE,
      WA_AMI_CAR_TYPE TYPE AMI_CAR_TYPE,
      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: <AMI_CAR_TYPE> TYPE AMI_CAR_TYPE.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM AMI_CAR_TYPE
*  INTO TABLE @DATA(IT_AMI_CAR_TYPE2).
*--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_AMI_CAR_TYPE INDEX 1 INTO DATA(WA_AMI_CAR_TYPE2).


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

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_AMI_CAR_TYPE-PICK_UP_LOCATION, sy-vline,
WA_AMI_CAR_TYPE-CONTROLLER, sy-vline,
WA_AMI_CAR_TYPE-BASE, sy-vline,
WA_AMI_CAR_TYPE-CONTROLLER, sy-vline,
WA_AMI_CAR_TYPE-LOCATION_CODE, sy-vline,
WA_AMI_CAR_TYPE-CONTROLLER, sy-vline.
ENDLOOP. *Add any further fields from structure WA_AMI_CAR_TYPE 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_AMI_CAR_TYPE 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_AMI_CAR_TYPE INTO WA_AMI_CAR_TYPE. *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_AMI_CAR_TYPE_STR,
CONTROLLER TYPE STRING,
TYPE TYPE STRING,
CONTROLLER TYPE STRING,
TYPE TYPE STRING,
THIRD_PARTY_INFO TYPE STRING,
PICK_UP_LOCATION TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
LOCATION_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
CITY_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
COUNTRY_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
STATE_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
ADDRESS TYPE STRING,
CONTROLLER TYPE STRING,
ADDRESS_LINE TYPE STRING,
ZIP_CODE TYPE STRING,
CITY TYPE STRING,
STATE TYPE STRING,
COUNTRY TYPE STRING,
GEO_CODE TYPE STRING,
CONTROLLER TYPE STRING,
LATITUDE TYPE STRING,
LONGITUDE TYPE STRING,
DROP_OFF_LOCATION TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
LOCATION_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
CITY_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
COUNTRY_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
STATE_CODE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
ADDRESS TYPE STRING,
CONTROLLER TYPE STRING,
ADDRESS_LINE TYPE STRING,
ZIP_CODE TYPE STRING,
CITY TYPE STRING,
STATE TYPE STRING,
COUNTRY TYPE STRING,
GEO_CODE TYPE STRING,
CONTROLLER TYPE STRING,
LATITUDE TYPE STRING,
LONGITUDE TYPE STRING,
PICK_UP_DATE_TIME TYPE STRING,
DROP_OFF_DATE_TIME TYPE STRING,
CAR_INFO TYPE STRING,
CONTROLLER TYPE STRING,
COMPANY TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
CLASS TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
TYPE TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
TRANSMISSION TYPE STRING,
AIR_CONDITIONING TYPE STRING,
CAR_FARE TYPE STRING,
LOWEST_FARE TYPE STRING,
JUSTIFICATION TYPE STRING,
AGENCY_PICK_UP_LOCATION TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
NAME TYPE STRING,
ADDRESS TYPE STRING,
CONTROLLER TYPE STRING,
ADDRESS_LINE TYPE STRING,
ZIP_CODE TYPE STRING,
CITY TYPE STRING,
STATE TYPE STRING,
COUNTRY TYPE STRING,
GEO_CODE TYPE STRING,
CONTROLLER TYPE STRING,
LATITUDE TYPE STRING,
LONGITUDE TYPE STRING,
PHONE TYPE STRING,
FAX TYPE STRING,
EMAIL TYPE STRING,
TELEX TYPE STRING,
LOCATION_CODE TYPE STRING,
COMPANY_LOCATION_CODE TYPE STRING,
OPENING_HOURS TYPE STRING,
CONTROLLER TYPE STRING,
OPENING TYPE STRING,
CLOSING TYPE STRING,
AGENCY_DROP_OFF_LOCATION TYPE STRING,
CONTROLLER TYPE STRING,
BASE TYPE STRING,
CONTROLLER TYPE STRING,
NAME TYPE STRING,
ADDRESS TYPE STRING,
CONTROLLER TYPE STRING,
ADDRESS_LINE TYPE STRING,
ZIP_CODE TYPE STRING,
CITY TYPE STRING,
STATE TYPE STRING,
COUNTRY TYPE STRING,
GEO_CODE TYPE STRING,
CONTROLLER TYPE STRING,
LATITUDE TYPE STRING,
LONGITUDE TYPE STRING,
PHONE TYPE STRING,
FAX TYPE STRING,
EMAIL TYPE STRING,
TELEX TYPE STRING,
LOCATION_CODE TYPE STRING,
COMPANY_LOCATION_CODE TYPE STRING,
OPENING_HOURS TYPE STRING,
CONTROLLER TYPE STRING,
OPENING TYPE STRING,
CLOSING TYPE STRING,
GUARANTEE TYPE STRING,
CONTROLLER TYPE STRING,
PAYMENT_TYPE TYPE STRING,
PAYMENT_NUMBER TYPE STRING,
PRICE_PLAN TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
STATUS TYPE STRING,
CONTROLLER TYPE STRING,
CODE TYPE STRING,
NAME TYPE STRING,
PERMISSIONS TYPE STRING,
CONTROLLER TYPE STRING,
IS_MODIFICATION_ALLOWED TYPE STRING,
IS_CANCELLATION_ALLOWED TYPE STRING,
IS_DATE_CHANGE_ONLY_ALLOWED TYPE STRING,
CONFIRMATION_NUMBER TYPE STRING,
CORPORATE_DISCOUNT_NUMBER TYPE STRING,
FREQUENT_RENTER_NUMBER TYPE STRING,
FLIGHT_INFO TYPE STRING,
ID TYPE STRING,
IS_OUT_OF_POLICY TYPE STRING,
IS_PAID_BY_CREDIT_CARD TYPE STRING,END OF T_EKKO_STR. DATA: WA_AMI_CAR_TYPE_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_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-TYPE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-TYPE sy-vline
WA_AMI_CAR_TYPE_STR-THIRD_PARTY_INFO sy-vline
WA_AMI_CAR_TYPE_STR-PICK_UP_LOCATION sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-BASE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-LOCATION_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-CITY_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-COUNTRY_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-STATE_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS_LINE sy-vline
WA_AMI_CAR_TYPE_STR-ZIP_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CITY sy-vline
WA_AMI_CAR_TYPE_STR-STATE sy-vline
WA_AMI_CAR_TYPE_STR-COUNTRY sy-vline
WA_AMI_CAR_TYPE_STR-GEO_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-LATITUDE sy-vline
WA_AMI_CAR_TYPE_STR-LONGITUDE sy-vline
WA_AMI_CAR_TYPE_STR-DROP_OFF_LOCATION sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-BASE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-LOCATION_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-CITY_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-COUNTRY_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-STATE_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS_LINE sy-vline
WA_AMI_CAR_TYPE_STR-ZIP_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CITY sy-vline
WA_AMI_CAR_TYPE_STR-STATE sy-vline
WA_AMI_CAR_TYPE_STR-COUNTRY sy-vline
WA_AMI_CAR_TYPE_STR-GEO_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-LATITUDE sy-vline
WA_AMI_CAR_TYPE_STR-LONGITUDE sy-vline
WA_AMI_CAR_TYPE_STR-PICK_UP_DATE_TIME sy-vline
WA_AMI_CAR_TYPE_STR-DROP_OFF_DATE_TIME sy-vline
WA_AMI_CAR_TYPE_STR-CAR_INFO sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-COMPANY sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-CLASS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-TYPE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-TRANSMISSION sy-vline
WA_AMI_CAR_TYPE_STR-AIR_CONDITIONING sy-vline
WA_AMI_CAR_TYPE_STR-CAR_FARE sy-vline
WA_AMI_CAR_TYPE_STR-LOWEST_FARE sy-vline
WA_AMI_CAR_TYPE_STR-JUSTIFICATION sy-vline
WA_AMI_CAR_TYPE_STR-AGENCY_PICK_UP_LOCATION sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-BASE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS_LINE sy-vline
WA_AMI_CAR_TYPE_STR-ZIP_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CITY sy-vline
WA_AMI_CAR_TYPE_STR-STATE sy-vline
WA_AMI_CAR_TYPE_STR-COUNTRY sy-vline
WA_AMI_CAR_TYPE_STR-GEO_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-LATITUDE sy-vline
WA_AMI_CAR_TYPE_STR-LONGITUDE sy-vline
WA_AMI_CAR_TYPE_STR-PHONE sy-vline
WA_AMI_CAR_TYPE_STR-FAX sy-vline
WA_AMI_CAR_TYPE_STR-EMAIL sy-vline
WA_AMI_CAR_TYPE_STR-TELEX sy-vline
WA_AMI_CAR_TYPE_STR-LOCATION_CODE sy-vline
WA_AMI_CAR_TYPE_STR-COMPANY_LOCATION_CODE sy-vline
WA_AMI_CAR_TYPE_STR-OPENING_HOURS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-OPENING sy-vline
WA_AMI_CAR_TYPE_STR-CLOSING sy-vline
WA_AMI_CAR_TYPE_STR-AGENCY_DROP_OFF_LOCATION sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-BASE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-ADDRESS_LINE sy-vline
WA_AMI_CAR_TYPE_STR-ZIP_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CITY sy-vline
WA_AMI_CAR_TYPE_STR-STATE sy-vline
WA_AMI_CAR_TYPE_STR-COUNTRY sy-vline
WA_AMI_CAR_TYPE_STR-GEO_CODE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-LATITUDE sy-vline
WA_AMI_CAR_TYPE_STR-LONGITUDE sy-vline
WA_AMI_CAR_TYPE_STR-PHONE sy-vline
WA_AMI_CAR_TYPE_STR-FAX sy-vline
WA_AMI_CAR_TYPE_STR-EMAIL sy-vline
WA_AMI_CAR_TYPE_STR-TELEX sy-vline
WA_AMI_CAR_TYPE_STR-LOCATION_CODE sy-vline
WA_AMI_CAR_TYPE_STR-COMPANY_LOCATION_CODE sy-vline
WA_AMI_CAR_TYPE_STR-OPENING_HOURS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-OPENING sy-vline
WA_AMI_CAR_TYPE_STR-CLOSING sy-vline
WA_AMI_CAR_TYPE_STR-GUARANTEE sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-PAYMENT_TYPE sy-vline
WA_AMI_CAR_TYPE_STR-PAYMENT_NUMBER sy-vline
WA_AMI_CAR_TYPE_STR-PRICE_PLAN sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-STATUS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-CODE sy-vline
WA_AMI_CAR_TYPE_STR-NAME sy-vline
WA_AMI_CAR_TYPE_STR-PERMISSIONS sy-vline
WA_AMI_CAR_TYPE_STR-CONTROLLER sy-vline
WA_AMI_CAR_TYPE_STR-IS_MODIFICATION_ALLOWED sy-vline
WA_AMI_CAR_TYPE_STR-IS_CANCELLATION_ALLOWED sy-vline
WA_AMI_CAR_TYPE_STR-IS_DATE_CHANGE_ONLY_ALLOWED sy-vline
WA_AMI_CAR_TYPE_STR-CONFIRMATION_NUMBER sy-vline
WA_AMI_CAR_TYPE_STR-CORPORATE_DISCOUNT_NUMBER sy-vline
WA_AMI_CAR_TYPE_STR-FREQUENT_RENTER_NUMBER sy-vline
WA_AMI_CAR_TYPE_STR-FLIGHT_INFO sy-vline
WA_AMI_CAR_TYPE_STR-ID sy-vline
WA_AMI_CAR_TYPE_STR-IS_OUT_OF_POLICY sy-vline
WA_AMI_CAR_TYPE_STR-IS_PAID_BY_CREDIT_CARD sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.