ABAP Select data from SAP table PC2R6 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 PC2R6 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 PC2R6. 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 PC2R6 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_PC2R6 TYPE STANDARD TABLE OF PC2R6,
      WA_PC2R6 TYPE PC2R6,
      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: <PC2R6> TYPE PC2R6.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PC2R6
*  INTO TABLE @DATA(IT_PC2R62).
*--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_PC2R6 INDEX 1 INTO DATA(WA_PC2R62).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PC2R6 ASSIGNING <PC2R6>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PC2R6>-TPASI = 1.
<PC2R6>-CDCOI = 1.
<PC2R6>-CDDTR = 1.
<PC2R6>-TPCM1 = 1.
<PC2R6>-NUCA1 = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PC2R6-PRCO1, sy-vline,
WA_PC2R6-XRIC1, sy-vline,
WA_PC2R6-TPCM2, sy-vline,
WA_PC2R6-NUCA2, sy-vline,
WA_PC2R6-PRCO2, sy-vline,
WA_PC2R6-XRIC2, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PC2R6 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_PC2R6 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_PC2R6 INTO WA_PC2R6. *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_PC2R6_STR,
TPASI TYPE STRING,
CDCOI TYPE STRING,
CDDTR TYPE STRING,
TPCM1 TYPE STRING,
NUCA1 TYPE STRING,
PRCO1 TYPE STRING,
XRIC1 TYPE STRING,
TPCM2 TYPE STRING,
NUCA2 TYPE STRING,
PRCO2 TYPE STRING,
XRIC2 TYPE STRING,
TPCM3 TYPE STRING,
NUCA3 TYPE STRING,
PRCO3 TYPE STRING,
XRIC3 TYPE STRING,
TPCM4 TYPE STRING,
NUCA4 TYPE STRING,
PRCO4 TYPE STRING,
XRIC4 TYPE STRING,
TPCM5 TYPE STRING,
NUCA5 TYPE STRING,
PRCO5 TYPE STRING,
XRIC5 TYPE STRING,
TPCM6 TYPE STRING,
NUCA6 TYPE STRING,
PRCO6 TYPE STRING,
XRIC6 TYPE STRING,
TPCM7 TYPE STRING,
NUCA7 TYPE STRING,
PRCO7 TYPE STRING,
XRIC7 TYPE STRING,
TPCM8 TYPE STRING,
NUCA8 TYPE STRING,
PRCO8 TYPE STRING,
XRIC8 TYPE STRING,
TPCM9 TYPE STRING,
NUCA9 TYPE STRING,
PRCO9 TYPE STRING,
XRIC9 TYPE STRING,
TPC10 TYPE STRING,
NUC10 TYPE STRING,
PRC10 TYPE STRING,
XRI10 TYPE STRING,
TPDE1 TYPE STRING,
XRTD1 TYPE STRING,
TPDE2 TYPE STRING,
XRTD2 TYPE STRING,
TPDE3 TYPE STRING,
XRTD3 TYPE STRING,
TPDE4 TYPE STRING,
XRTD4 TYPE STRING,
TPDE5 TYPE STRING,
XRTD5 TYPE STRING,
TPON1 TYPE STRING,
XROD1 TYPE STRING,
TPON2 TYPE STRING,
XROD2 TYPE STRING,
TPON3 TYPE STRING,
XROD3 TYPE STRING,
TPON4 TYPE STRING,
XROD4 TYPE STRING,
TPON5 TYPE STRING,
XROD5 TYPE STRING,
CDALT TYPE STRING,
CDTRE TYPE STRING,
REGIO TYPE STRING,
PROVI TYPE STRING,
CDTPR TYPE STRING,
MUNIC TYPE STRING,
CDTMU TYPE STRING,
CDEDU TYPE STRING,
DTMIN TYPE STRING,
APAIM TYPE STRING,
BBOPT TYPE STRING,END OF T_EKKO_STR. DATA: WA_PC2R6_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_PC2R6_STR-TPASI sy-vline
WA_PC2R6_STR-CDCOI sy-vline
WA_PC2R6_STR-CDDTR sy-vline
WA_PC2R6_STR-TPCM1 sy-vline
WA_PC2R6_STR-NUCA1 sy-vline
WA_PC2R6_STR-PRCO1 sy-vline
WA_PC2R6_STR-XRIC1 sy-vline
WA_PC2R6_STR-TPCM2 sy-vline
WA_PC2R6_STR-NUCA2 sy-vline
WA_PC2R6_STR-PRCO2 sy-vline
WA_PC2R6_STR-XRIC2 sy-vline
WA_PC2R6_STR-TPCM3 sy-vline
WA_PC2R6_STR-NUCA3 sy-vline
WA_PC2R6_STR-PRCO3 sy-vline
WA_PC2R6_STR-XRIC3 sy-vline
WA_PC2R6_STR-TPCM4 sy-vline
WA_PC2R6_STR-NUCA4 sy-vline
WA_PC2R6_STR-PRCO4 sy-vline
WA_PC2R6_STR-XRIC4 sy-vline
WA_PC2R6_STR-TPCM5 sy-vline
WA_PC2R6_STR-NUCA5 sy-vline
WA_PC2R6_STR-PRCO5 sy-vline
WA_PC2R6_STR-XRIC5 sy-vline
WA_PC2R6_STR-TPCM6 sy-vline
WA_PC2R6_STR-NUCA6 sy-vline
WA_PC2R6_STR-PRCO6 sy-vline
WA_PC2R6_STR-XRIC6 sy-vline
WA_PC2R6_STR-TPCM7 sy-vline
WA_PC2R6_STR-NUCA7 sy-vline
WA_PC2R6_STR-PRCO7 sy-vline
WA_PC2R6_STR-XRIC7 sy-vline
WA_PC2R6_STR-TPCM8 sy-vline
WA_PC2R6_STR-NUCA8 sy-vline
WA_PC2R6_STR-PRCO8 sy-vline
WA_PC2R6_STR-XRIC8 sy-vline
WA_PC2R6_STR-TPCM9 sy-vline
WA_PC2R6_STR-NUCA9 sy-vline
WA_PC2R6_STR-PRCO9 sy-vline
WA_PC2R6_STR-XRIC9 sy-vline
WA_PC2R6_STR-TPC10 sy-vline
WA_PC2R6_STR-NUC10 sy-vline
WA_PC2R6_STR-PRC10 sy-vline
WA_PC2R6_STR-XRI10 sy-vline
WA_PC2R6_STR-TPDE1 sy-vline
WA_PC2R6_STR-XRTD1 sy-vline
WA_PC2R6_STR-TPDE2 sy-vline
WA_PC2R6_STR-XRTD2 sy-vline
WA_PC2R6_STR-TPDE3 sy-vline
WA_PC2R6_STR-XRTD3 sy-vline
WA_PC2R6_STR-TPDE4 sy-vline
WA_PC2R6_STR-XRTD4 sy-vline
WA_PC2R6_STR-TPDE5 sy-vline
WA_PC2R6_STR-XRTD5 sy-vline
WA_PC2R6_STR-TPON1 sy-vline
WA_PC2R6_STR-XROD1 sy-vline
WA_PC2R6_STR-TPON2 sy-vline
WA_PC2R6_STR-XROD2 sy-vline
WA_PC2R6_STR-TPON3 sy-vline
WA_PC2R6_STR-XROD3 sy-vline
WA_PC2R6_STR-TPON4 sy-vline
WA_PC2R6_STR-XROD4 sy-vline
WA_PC2R6_STR-TPON5 sy-vline
WA_PC2R6_STR-XROD5 sy-vline
WA_PC2R6_STR-CDALT sy-vline
WA_PC2R6_STR-CDTRE sy-vline
WA_PC2R6_STR-REGIO sy-vline
WA_PC2R6_STR-PROVI sy-vline
WA_PC2R6_STR-CDTPR sy-vline
WA_PC2R6_STR-MUNIC sy-vline
WA_PC2R6_STR-CDTMU sy-vline
WA_PC2R6_STR-CDEDU sy-vline
WA_PC2R6_STR-DTMIN sy-vline
WA_PC2R6_STR-APAIM sy-vline
WA_PC2R6_STR-BBOPT sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.