ABAP Select data from SAP table QAMRR 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 QAMRR 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 QAMRR. 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 QAMRR 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_QAMRR TYPE STANDARD TABLE OF QAMRR,
      WA_QAMRR TYPE QAMRR,
      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: <QAMRR> TYPE QAMRR.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM QAMRR
*  INTO TABLE @DATA(IT_QAMRR2).
*--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_QAMRR INDEX 1 INTO DATA(WA_QAMRR2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_QAMRR ASSIGNING <QAMRR>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<QAMRR>-UPSLR = 1.
<QAMRR>-STATUSR = 1.
<QAMRR>-ATTRIBUT = 1.
<QAMRR>-ERSTELLER = 1.
<QAMRR>-ERSTELLDAT = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_QAMRR-AENDERER, sy-vline,
WA_QAMRR-AENDERDAT, sy-vline,
WA_QAMRR-DBEWERTG, sy-vline,
WA_QAMRR-MBEWERTG, sy-vline,
WA_QAMRR-PRUEFER, sy-vline,
WA_QAMRR-PRUEFDATUV, sy-vline.
ENDLOOP. *Add any further fields from structure WA_QAMRR 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_QAMRR 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_QAMRR INTO WA_QAMRR. *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 ISOLA, internal->external for field LTEXTSPR CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_QAMRR-LTEXTSPR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_QAMRR-LTEXTSPR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

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

*Conversion exit ALPHA, internal->external for field MASCHINE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_QAMRR-MASCHINE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_QAMRR-MASCHINE.
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_QAMRR_STR,
UPSLR TYPE STRING,
STATUSR TYPE STRING,
ATTRIBUT TYPE STRING,
ERSTELLER TYPE STRING,
ERSTELLDAT TYPE STRING,
AENDERER TYPE STRING,
AENDERDAT TYPE STRING,
DBEWERTG TYPE STRING,
MBEWERTG TYPE STRING,
PRUEFER TYPE STRING,
PRUEFDATUV TYPE STRING,
PRUEFDATUB TYPE STRING,
PRUEFZEITV TYPE STRING,
PRUEFZEITB TYPE STRING,
PRUEFBEMKT TYPE STRING,
PRLTEXTKZ TYPE STRING,
LTEXTSPR TYPE STRING,
ISTSTPANZ TYPE STRING,
ISTSTPUMF TYPE STRING,
ANZFEHLEH TYPE STRING,
ANTEILNI TYPE STRING,
ANTEIL TYPE STRING,
ANZFEHLER TYPE STRING,
ANZWERTO TYPE STRING,
ANZWERTU TYPE STRING,
ANZWERTG TYPE STRING,
MAXWERTNI TYPE STRING,
MEDIANNI TYPE STRING,
MINWERTNI TYPE STRING,
MITTELWNI TYPE STRING,
VARIANZNI TYPE STRING,
MOMENT3NI TYPE STRING,
MOMENT4NI TYPE STRING,
ANTEILONI TYPE STRING,
ANTEILUNI TYPE STRING,
MAXWERT TYPE STRING,
MEDIANWERT TYPE STRING,
MINWERT TYPE STRING,
MITTELWERT TYPE STRING,
VARIANZ TYPE STRING,
MOMENT3 TYPE STRING,
MOMENT4 TYPE STRING,
ANTEILO TYPE STRING,
ANTEILU TYPE STRING,
GUELSTPANZ TYPE STRING,
IVARIANZNI TYPE STRING,
IVARIANZ TYPE STRING,
GRUPPE1 TYPE STRING,
CODE1 TYPE STRING,
VERSION1 TYPE STRING,
GRUPPE2 TYPE STRING,
CODE2 TYPE STRING,
VERSION2 TYPE STRING,
GRUPPE3 TYPE STRING,
CODE3 TYPE STRING,
VERSION3 TYPE STRING,
GRUPPE4 TYPE STRING,
CODE4 TYPE STRING,
VERSION4 TYPE STRING,
GRUPPE5 TYPE STRING,
CODE5 TYPE STRING,
VERSION5 TYPE STRING,
FEHLKLAS TYPE STRING,
STDABWNI TYPE STRING,
STDABW TYPE STRING,
SPANNWEINI TYPE STRING,
SPANNWEITE TYPE STRING,
SENDEFLAG TYPE STRING,
LASTDETAIL TYPE STRING,
LASTPROBE TYPE STRING,
MASCHINE TYPE STRING,
POSITION TYPE STRING,
KATERG TYPE STRING,END OF T_EKKO_STR. DATA: WA_QAMRR_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_QAMRR_STR-UPSLR sy-vline
WA_QAMRR_STR-STATUSR sy-vline
WA_QAMRR_STR-ATTRIBUT sy-vline
WA_QAMRR_STR-ERSTELLER sy-vline
WA_QAMRR_STR-ERSTELLDAT sy-vline
WA_QAMRR_STR-AENDERER sy-vline
WA_QAMRR_STR-AENDERDAT sy-vline
WA_QAMRR_STR-DBEWERTG sy-vline
WA_QAMRR_STR-MBEWERTG sy-vline
WA_QAMRR_STR-PRUEFER sy-vline
WA_QAMRR_STR-PRUEFDATUV sy-vline
WA_QAMRR_STR-PRUEFDATUB sy-vline
WA_QAMRR_STR-PRUEFZEITV sy-vline
WA_QAMRR_STR-PRUEFZEITB sy-vline
WA_QAMRR_STR-PRUEFBEMKT sy-vline
WA_QAMRR_STR-PRLTEXTKZ sy-vline
WA_QAMRR_STR-LTEXTSPR sy-vline
WA_QAMRR_STR-ISTSTPANZ sy-vline
WA_QAMRR_STR-ISTSTPUMF sy-vline
WA_QAMRR_STR-ANZFEHLEH sy-vline
WA_QAMRR_STR-ANTEILNI sy-vline
WA_QAMRR_STR-ANTEIL sy-vline
WA_QAMRR_STR-ANZFEHLER sy-vline
WA_QAMRR_STR-ANZWERTO sy-vline
WA_QAMRR_STR-ANZWERTU sy-vline
WA_QAMRR_STR-ANZWERTG sy-vline
WA_QAMRR_STR-MAXWERTNI sy-vline
WA_QAMRR_STR-MEDIANNI sy-vline
WA_QAMRR_STR-MINWERTNI sy-vline
WA_QAMRR_STR-MITTELWNI sy-vline
WA_QAMRR_STR-VARIANZNI sy-vline
WA_QAMRR_STR-MOMENT3NI sy-vline
WA_QAMRR_STR-MOMENT4NI sy-vline
WA_QAMRR_STR-ANTEILONI sy-vline
WA_QAMRR_STR-ANTEILUNI sy-vline
WA_QAMRR_STR-MAXWERT sy-vline
WA_QAMRR_STR-MEDIANWERT sy-vline
WA_QAMRR_STR-MINWERT sy-vline
WA_QAMRR_STR-MITTELWERT sy-vline
WA_QAMRR_STR-VARIANZ sy-vline
WA_QAMRR_STR-MOMENT3 sy-vline
WA_QAMRR_STR-MOMENT4 sy-vline
WA_QAMRR_STR-ANTEILO sy-vline
WA_QAMRR_STR-ANTEILU sy-vline
WA_QAMRR_STR-GUELSTPANZ sy-vline
WA_QAMRR_STR-IVARIANZNI sy-vline
WA_QAMRR_STR-IVARIANZ sy-vline
WA_QAMRR_STR-GRUPPE1 sy-vline
WA_QAMRR_STR-CODE1 sy-vline
WA_QAMRR_STR-VERSION1 sy-vline
WA_QAMRR_STR-GRUPPE2 sy-vline
WA_QAMRR_STR-CODE2 sy-vline
WA_QAMRR_STR-VERSION2 sy-vline
WA_QAMRR_STR-GRUPPE3 sy-vline
WA_QAMRR_STR-CODE3 sy-vline
WA_QAMRR_STR-VERSION3 sy-vline
WA_QAMRR_STR-GRUPPE4 sy-vline
WA_QAMRR_STR-CODE4 sy-vline
WA_QAMRR_STR-VERSION4 sy-vline
WA_QAMRR_STR-GRUPPE5 sy-vline
WA_QAMRR_STR-CODE5 sy-vline
WA_QAMRR_STR-VERSION5 sy-vline
WA_QAMRR_STR-FEHLKLAS sy-vline
WA_QAMRR_STR-STDABWNI sy-vline
WA_QAMRR_STR-STDABW sy-vline
WA_QAMRR_STR-SPANNWEINI sy-vline
WA_QAMRR_STR-SPANNWEITE sy-vline
WA_QAMRR_STR-SENDEFLAG sy-vline
WA_QAMRR_STR-LASTDETAIL sy-vline
WA_QAMRR_STR-LASTPROBE sy-vline
WA_QAMRR_STR-MASCHINE sy-vline
WA_QAMRR_STR-POSITION sy-vline
WA_QAMRR_STR-KATERG sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.