ABAP Select data from SAP table QPMK_CBC 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 QPMK_CBC 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 QPMK_CBC. 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 QPMK_CBC 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_QPMK_CBC TYPE STANDARD TABLE OF QPMK_CBC,
      WA_QPMK_CBC TYPE QPMK_CBC,
      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: <QPMK_CBC> TYPE QPMK_CBC.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM QPMK_CBC
*  INTO TABLE @DATA(IT_QPMK_CBC2).
*--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_QPMK_CBC INDEX 1 INTO DATA(WA_QPMK_CBC2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_QPMK_CBC ASSIGNING <QPMK_CBC>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<QPMK_CBC>-I_MANDT = 1.
<QPMK_CBC>-I_MKMNR = 1.
<QPMK_CBC>-I_WERKS = 1.
<QPMK_CBC>-I_AENDBELEG = 1.
<QPMK_CBC>-I_ATNAM = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_QPMK_CBC-I_AUSWMENGE1, sy-vline,
WA_QPMK_CBC-I_AUSWMGWRK1, sy-vline,
WA_QPMK_CBC-I_CODE9O, sy-vline,
WA_QPMK_CBC-I_CODE9U, sy-vline,
WA_QPMK_CBC-I_CODEGR9O, sy-vline,
WA_QPMK_CBC-I_CODEGR9U, sy-vline.
ENDLOOP. *Add any further fields from structure WA_QPMK_CBC 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_QPMK_CBC 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_QPMK_CBC INTO WA_QPMK_CBC. *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 I_MKMNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_QPMK_CBC-I_MKMNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_QPMK_CBC-I_MKMNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit LUNIT, internal->external for field I_MASSEINHSW CALL FUNCTION 'CONVERSION_EXIT_LUNIT_OUTPUT' EXPORTING input = WA_QPMK_CBC-I_MASSEINHSW IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_QPMK_CBC-I_MASSEINHSW.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit ALPHA, internal->external for field I_PMVERSION CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_QPMK_CBC-I_PMVERSION IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_QPMK_CBC-I_PMVERSION.
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_QPMK_CBC_STR,
I_MANDT TYPE STRING,
I_MKMNR TYPE STRING,
I_WERKS TYPE STRING,
I_AENDBELEG TYPE STRING,
I_ATNAM TYPE STRING,
I_AUSWMENGE1 TYPE STRING,
I_AUSWMGWRK1 TYPE STRING,
I_CODE9O TYPE STRING,
I_CODE9U TYPE STRING,
I_CODEGR9O TYPE STRING,
I_CODEGR9U TYPE STRING,
I_CODEGRQUAL TYPE STRING,
I_CODEQUAL TYPE STRING,
I_DOKUKZ1 TYPE STRING,
I_DOKUKZ2 TYPE STRING,
I_DOKUKZ3 TYPE STRING,
I_DRUCK1 TYPE STRING,
I_DRUCK2 TYPE STRING,
I_EEANTVERF TYPE STRING,
I_ESTUKZ3 TYPE STRING,
I_ESTUKZ5 TYPE STRING,
I_FEHLREC TYPE STRING,
I_GUELTIGAB TYPE STRING,
I_KEINEFORMEL TYPE STRING,
I_KONSISTENT TYPE STRING,
I_KURZTEXT_1 TYPE STRING,
I_KURZTEXT_2 TYPE STRING,
I_LOEKZ TYPE STRING,
I_LZEITKZ TYPE STRING,
I_MASSEINHSW TYPE STRING,
I_MESSWERTE TYPE STRING,
I_PMTNR TYPE STRING,
I_PMVERSION TYPE STRING,
I_PMWERK TYPE STRING,
I_PRFQL TYPE STRING,
I_PRUEFKAT TYPE STRING,
I_PUMFKZ1 TYPE STRING,
I_PUMFKZ4 TYPE STRING,
I_QSPCMK TYPE STRING,
I_QUALITAET TYPE STRING,
I_QUANTITAET TYPE STRING,
I_RZWANG1 TYPE STRING,
I_RZWANG4 TYPE STRING,
I_SOLLPRUEF TYPE STRING,
I_SOLLWERT TYPE STRING,
I_SORTFELD TYPE STRING,
I_SPRA_1 TYPE STRING,
I_SPRA_2 TYPE STRING,
I_STELLEN TYPE STRING,
I_STICHPR TYPE STRING,
I_TOLERANZOB TYPE STRING,
I_TOLERANZUN TYPE STRING,
I_TOLEROBEN TYPE STRING,
I_TOLERUNTEN TYPE STRING,END OF T_EKKO_STR. DATA: WA_QPMK_CBC_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_QPMK_CBC_STR-I_MANDT sy-vline
WA_QPMK_CBC_STR-I_MKMNR sy-vline
WA_QPMK_CBC_STR-I_WERKS sy-vline
WA_QPMK_CBC_STR-I_AENDBELEG sy-vline
WA_QPMK_CBC_STR-I_ATNAM sy-vline
WA_QPMK_CBC_STR-I_AUSWMENGE1 sy-vline
WA_QPMK_CBC_STR-I_AUSWMGWRK1 sy-vline
WA_QPMK_CBC_STR-I_CODE9O sy-vline
WA_QPMK_CBC_STR-I_CODE9U sy-vline
WA_QPMK_CBC_STR-I_CODEGR9O sy-vline
WA_QPMK_CBC_STR-I_CODEGR9U sy-vline
WA_QPMK_CBC_STR-I_CODEGRQUAL sy-vline
WA_QPMK_CBC_STR-I_CODEQUAL sy-vline
WA_QPMK_CBC_STR-I_DOKUKZ1 sy-vline
WA_QPMK_CBC_STR-I_DOKUKZ2 sy-vline
WA_QPMK_CBC_STR-I_DOKUKZ3 sy-vline
WA_QPMK_CBC_STR-I_DRUCK1 sy-vline
WA_QPMK_CBC_STR-I_DRUCK2 sy-vline
WA_QPMK_CBC_STR-I_EEANTVERF sy-vline
WA_QPMK_CBC_STR-I_ESTUKZ3 sy-vline
WA_QPMK_CBC_STR-I_ESTUKZ5 sy-vline
WA_QPMK_CBC_STR-I_FEHLREC sy-vline
WA_QPMK_CBC_STR-I_GUELTIGAB sy-vline
WA_QPMK_CBC_STR-I_KEINEFORMEL sy-vline
WA_QPMK_CBC_STR-I_KONSISTENT sy-vline
WA_QPMK_CBC_STR-I_KURZTEXT_1 sy-vline
WA_QPMK_CBC_STR-I_KURZTEXT_2 sy-vline
WA_QPMK_CBC_STR-I_LOEKZ sy-vline
WA_QPMK_CBC_STR-I_LZEITKZ sy-vline
WA_QPMK_CBC_STR-I_MASSEINHSW sy-vline
WA_QPMK_CBC_STR-I_MESSWERTE sy-vline
WA_QPMK_CBC_STR-I_PMTNR sy-vline
WA_QPMK_CBC_STR-I_PMVERSION sy-vline
WA_QPMK_CBC_STR-I_PMWERK sy-vline
WA_QPMK_CBC_STR-I_PRFQL sy-vline
WA_QPMK_CBC_STR-I_PRUEFKAT sy-vline
WA_QPMK_CBC_STR-I_PUMFKZ1 sy-vline
WA_QPMK_CBC_STR-I_PUMFKZ4 sy-vline
WA_QPMK_CBC_STR-I_QSPCMK sy-vline
WA_QPMK_CBC_STR-I_QUALITAET sy-vline
WA_QPMK_CBC_STR-I_QUANTITAET sy-vline
WA_QPMK_CBC_STR-I_RZWANG1 sy-vline
WA_QPMK_CBC_STR-I_RZWANG4 sy-vline
WA_QPMK_CBC_STR-I_SOLLPRUEF sy-vline
WA_QPMK_CBC_STR-I_SOLLWERT sy-vline
WA_QPMK_CBC_STR-I_SORTFELD sy-vline
WA_QPMK_CBC_STR-I_SPRA_1 sy-vline
WA_QPMK_CBC_STR-I_SPRA_2 sy-vline
WA_QPMK_CBC_STR-I_STELLEN sy-vline
WA_QPMK_CBC_STR-I_STICHPR sy-vline
WA_QPMK_CBC_STR-I_TOLERANZOB sy-vline
WA_QPMK_CBC_STR-I_TOLERANZUN sy-vline
WA_QPMK_CBC_STR-I_TOLEROBEN sy-vline
WA_QPMK_CBC_STR-I_TOLERUNTEN sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.