ABAP Select data from SAP table MCQAQRB_BW 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 MCQAQRB_BW 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 MCQAQRB_BW. 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 MCQAQRB_BW 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_MCQAQRB_BW TYPE STANDARD TABLE OF MCQAQRB_BW,
      WA_MCQAQRB_BW TYPE MCQAQRB_BW,
      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: <MCQAQRB_BW> TYPE MCQAQRB_BW.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM MCQAQRB_BW
*  INTO TABLE @DATA(IT_MCQAQRB_BW2).
*--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_MCQAQRB_BW INDEX 1 INTO DATA(WA_MCQAQRB_BW2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_MCQAQRB_BW ASSIGNING <MCQAQRB_BW>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<MCQAQRB_BW>-ROCANCEL = 1.
<MCQAQRB_BW>-PRUEFLOS = 1.
<MCQAQRB_BW>-VORGLFNR = 1.
<MCQAQRB_BW>-MERKNR = 1.
<MCQAQRB_BW>-PROBENR = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_MCQAQRB_BW-QPMK_REF, sy-vline,
WA_MCQAQRB_BW-QPMK_WERKS, sy-vline,
WA_MCQAQRB_BW-VERWMERKM, sy-vline,
WA_MCQAQRB_BW-MERKGEW, sy-vline,
WA_MCQAQRB_BW-STATUSR, sy-vline,
WA_MCQAQRB_BW-MBEWERTG, sy-vline.
ENDLOOP. *Add any further fields from structure WA_MCQAQRB_BW 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_MCQAQRB_BW 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_MCQAQRB_BW INTO WA_MCQAQRB_BW. *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 ALPHA, internal->external for field PRUEFLOS CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_MCQAQRB_BW-PRUEFLOS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MCQAQRB_BW-PRUEFLOS.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit CUNIT, internal->external for field MASSEINHSW CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_MCQAQRB_BW-MASSEINHSW IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_MCQAQRB_BW-MASSEINHSW.
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_MCQAQRB_BW_STR,
ROCANCEL TYPE STRING,
PRUEFLOS TYPE STRING,
VORGLFNR TYPE STRING,
MERKNR TYPE STRING,
PROBENR TYPE STRING,
QPMK_REF TYPE STRING,
QPMK_WERKS TYPE STRING,
VERWMERKM TYPE STRING,
MERKGEW TYPE STRING,
STATUSR TYPE STRING,
MBEWERTG TYPE STRING,
ATTRIBUT TYPE STRING,
DUMMY10 TYPE STRING,
DUMMY20 TYPE STRING,
DUMMY40 TYPE STRING,
CODE1 TYPE STRING,
GRUPPE1 TYPE STRING,
KATALGART1 TYPE STRING,
PMETHODE TYPE STRING,
QMTB_WERKS TYPE STRING,
PMTVERSION TYPE STRING,
PRUEFBEMKT TYPE STRING,
PRLTEXTKZ TYPE STRING,
ANZFEHLEH TYPE STRING,
ANTEILNI TYPE STRING,
ANTEIL TYPE STRING,
ANZFEHLER TYPE STRING,
ANZWERTO TYPE STRING,
ANZWERTU TYPE STRING,
MITTELWNI TYPE STRING,
MITTELWERT TYPE STRING,
ANTEILO TYPE STRING,
ANTEILU TYPE STRING,
STEUERKZ TYPE STRING,
TSTICHPRKZ TYPE STRING,
MASSEINHSW TYPE STRING,
ZHLN TYPE STRING,
ZHLNA TYPE STRING,
ZHLNR TYPE STRING,
ZHLNSKIP TYPE STRING,
ZHLNSTPF TYPE STRING,
ZHLNKF TYPE STRING,
ZHLNHF TYPE STRING,
ZHLNNF TYPE STRING,
SUMFEHEH TYPE STRING,
SUMFANT TYPE STRING,
SIGMA TYPE STRING,
SIGMA_NKST TYPE STRING,
XQUER TYPE STRING,
XQUER_NKST TYPE STRING,
ZHLNSTAT TYPE STRING,
SUMEW TYPE STRING,
SUMMW TYPE STRING,
SUMVARIN TYPE STRING,
SUMSIGMAST TYPE STRING,
SUMVAR TYPE STRING,
TOLUN TYPE STRING,
BWTOLUNNI TYPE STRING,
TOLOB TYPE STRING,
BWTOLOBNI TYPE STRING,
TOL_NKST TYPE STRING,
CP TYPE STRING,
CPK TYPE STRING,
CP_NKST TYPE STRING,
SOLLWERT TYPE STRING,
BWSOLLWNI TYPE STRING,
GRENZEOB1 TYPE STRING,
BWGRENZEOB1NI TYPE STRING,
GRENZEUN1 TYPE STRING,
BWGRENZEUN1NI TYPE STRING,
GRENZEOB2 TYPE STRING,
BWGRENZEOB2NI TYPE STRING,
GRENZEUN2 TYPE STRING,
BWGRENZEUN2NI TYPE STRING,
PRUEFDATUV TYPE STRING,
NIPLANMKKZ TYPE STRING,END OF T_EKKO_STR. DATA: WA_MCQAQRB_BW_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_MCQAQRB_BW_STR-ROCANCEL sy-vline
WA_MCQAQRB_BW_STR-PRUEFLOS sy-vline
WA_MCQAQRB_BW_STR-VORGLFNR sy-vline
WA_MCQAQRB_BW_STR-MERKNR sy-vline
WA_MCQAQRB_BW_STR-PROBENR sy-vline
WA_MCQAQRB_BW_STR-QPMK_REF sy-vline
WA_MCQAQRB_BW_STR-QPMK_WERKS sy-vline
WA_MCQAQRB_BW_STR-VERWMERKM sy-vline
WA_MCQAQRB_BW_STR-MERKGEW sy-vline
WA_MCQAQRB_BW_STR-STATUSR sy-vline
WA_MCQAQRB_BW_STR-MBEWERTG sy-vline
WA_MCQAQRB_BW_STR-ATTRIBUT sy-vline
WA_MCQAQRB_BW_STR-DUMMY10 sy-vline
WA_MCQAQRB_BW_STR-DUMMY20 sy-vline
WA_MCQAQRB_BW_STR-DUMMY40 sy-vline
WA_MCQAQRB_BW_STR-CODE1 sy-vline
WA_MCQAQRB_BW_STR-GRUPPE1 sy-vline
WA_MCQAQRB_BW_STR-KATALGART1 sy-vline
WA_MCQAQRB_BW_STR-PMETHODE sy-vline
WA_MCQAQRB_BW_STR-QMTB_WERKS sy-vline
WA_MCQAQRB_BW_STR-PMTVERSION sy-vline
WA_MCQAQRB_BW_STR-PRUEFBEMKT sy-vline
WA_MCQAQRB_BW_STR-PRLTEXTKZ sy-vline
WA_MCQAQRB_BW_STR-ANZFEHLEH sy-vline
WA_MCQAQRB_BW_STR-ANTEILNI sy-vline
WA_MCQAQRB_BW_STR-ANTEIL sy-vline
WA_MCQAQRB_BW_STR-ANZFEHLER sy-vline
WA_MCQAQRB_BW_STR-ANZWERTO sy-vline
WA_MCQAQRB_BW_STR-ANZWERTU sy-vline
WA_MCQAQRB_BW_STR-MITTELWNI sy-vline
WA_MCQAQRB_BW_STR-MITTELWERT sy-vline
WA_MCQAQRB_BW_STR-ANTEILO sy-vline
WA_MCQAQRB_BW_STR-ANTEILU sy-vline
WA_MCQAQRB_BW_STR-STEUERKZ sy-vline
WA_MCQAQRB_BW_STR-TSTICHPRKZ sy-vline
WA_MCQAQRB_BW_STR-MASSEINHSW sy-vline
WA_MCQAQRB_BW_STR-ZHLN sy-vline
WA_MCQAQRB_BW_STR-ZHLNA sy-vline
WA_MCQAQRB_BW_STR-ZHLNR sy-vline
WA_MCQAQRB_BW_STR-ZHLNSKIP sy-vline
WA_MCQAQRB_BW_STR-ZHLNSTPF sy-vline
WA_MCQAQRB_BW_STR-ZHLNKF sy-vline
WA_MCQAQRB_BW_STR-ZHLNHF sy-vline
WA_MCQAQRB_BW_STR-ZHLNNF sy-vline
WA_MCQAQRB_BW_STR-SUMFEHEH sy-vline
WA_MCQAQRB_BW_STR-SUMFANT sy-vline
WA_MCQAQRB_BW_STR-SIGMA sy-vline
WA_MCQAQRB_BW_STR-SIGMA_NKST sy-vline
WA_MCQAQRB_BW_STR-XQUER sy-vline
WA_MCQAQRB_BW_STR-XQUER_NKST sy-vline
WA_MCQAQRB_BW_STR-ZHLNSTAT sy-vline
WA_MCQAQRB_BW_STR-SUMEW sy-vline
WA_MCQAQRB_BW_STR-SUMMW sy-vline
WA_MCQAQRB_BW_STR-SUMVARIN sy-vline
WA_MCQAQRB_BW_STR-SUMSIGMAST sy-vline
WA_MCQAQRB_BW_STR-SUMVAR sy-vline
WA_MCQAQRB_BW_STR-TOLUN sy-vline
WA_MCQAQRB_BW_STR-BWTOLUNNI sy-vline
WA_MCQAQRB_BW_STR-TOLOB sy-vline
WA_MCQAQRB_BW_STR-BWTOLOBNI sy-vline
WA_MCQAQRB_BW_STR-TOL_NKST sy-vline
WA_MCQAQRB_BW_STR-CP sy-vline
WA_MCQAQRB_BW_STR-CPK sy-vline
WA_MCQAQRB_BW_STR-CP_NKST sy-vline
WA_MCQAQRB_BW_STR-SOLLWERT sy-vline
WA_MCQAQRB_BW_STR-BWSOLLWNI sy-vline
WA_MCQAQRB_BW_STR-GRENZEOB1 sy-vline
WA_MCQAQRB_BW_STR-BWGRENZEOB1NI sy-vline
WA_MCQAQRB_BW_STR-GRENZEUN1 sy-vline
WA_MCQAQRB_BW_STR-BWGRENZEUN1NI sy-vline
WA_MCQAQRB_BW_STR-GRENZEOB2 sy-vline
WA_MCQAQRB_BW_STR-BWGRENZEOB2NI sy-vline
WA_MCQAQRB_BW_STR-GRENZEUN2 sy-vline
WA_MCQAQRB_BW_STR-BWGRENZEUN2NI sy-vline
WA_MCQAQRB_BW_STR-PRUEFDATUV sy-vline
WA_MCQAQRB_BW_STR-NIPLANMKKZ sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.