ABAP Select data from SAP table P2RD_STZ 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 P2RD_STZ 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 P2RD_STZ. 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 P2RD_STZ 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_P2RD_STZ TYPE STANDARD TABLE OF P2RD_STZ,
      WA_P2RD_STZ TYPE P2RD_STZ,
      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: <P2RD_STZ> TYPE P2RD_STZ.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM P2RD_STZ
*  INTO TABLE @DATA(IT_P2RD_STZ2).
*--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_P2RD_STZ INDEX 1 INTO DATA(WA_P2RD_STZ2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_P2RD_STZ ASSIGNING <P2RD_STZ>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<P2RD_STZ>-MANDT = 1.
<P2RD_STZ>-DCT_PERNR = 1.
<P2RD_STZ>-DCT_SEQNR = 1.
<P2RD_STZ>-BEGVB = 1.
<P2RD_STZ>-ANZVB = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_P2RD_STZ-VBEZL, sy-vline,
WA_P2RD_STZ-VBEZS, sy-vline,
WA_P2RD_STZ-VBEZB, sy-vline,
WA_P2RD_STZ-FBVBZ, sy-vline,
WA_P2RD_STZ-AEBEJ, sy-vline,
WA_P2RD_STZ-ENDVB, sy-vline.
ENDLOOP. *Add any further fields from structure WA_P2RD_STZ 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_P2RD_STZ 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_P2RD_STZ INTO WA_P2RD_STZ. *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_P2RD_STZ_STR,
MANDT TYPE STRING,
DCT_PERNR TYPE STRING,
DCT_SEQNR TYPE STRING,
BEGVB TYPE STRING,
ANZVB TYPE STRING,
VBEZL TYPE STRING,
VBEZS TYPE STRING,
VBEZB TYPE STRING,
FBVBZ TYPE STRING,
AEBEJ TYPE STRING,
ENDVB TYPE STRING,
KJAHR TYPE STRING,
LGVBZ TYPE STRING,
BEGV2 TYPE STRING,
ANZV2 TYPE STRING,
VBE2L TYPE STRING,
VBE2S TYPE STRING,
VBEZ2 TYPE STRING,
FBVB2 TYPE STRING,
ENDV2 TYPE STRING,
KJAH2 TYPE STRING,
LGVB2 TYPE STRING,
BEGV3 TYPE STRING,
ANZV3 TYPE STRING,
VBE3L TYPE STRING,
VBE3S TYPE STRING,
VBEZ3 TYPE STRING,
FBVB3 TYPE STRING,
ENDV3 TYPE STRING,
KJAH3 TYPE STRING,
LGVB3 TYPE STRING,
BEGV4 TYPE STRING,
ANZV4 TYPE STRING,
VBE4L TYPE STRING,
VBE4S TYPE STRING,
VBEZ4 TYPE STRING,
FBVB4 TYPE STRING,
ENDV4 TYPE STRING,
KJAH4 TYPE STRING,
LGVB4 TYPE STRING,
BEGV5 TYPE STRING,
ANZV5 TYPE STRING,
VBE5L TYPE STRING,
VBE5S TYPE STRING,
VBEZ5 TYPE STRING,
FBVB5 TYPE STRING,
ENDV5 TYPE STRING,
KJAH5 TYPE STRING,
LGVB5 TYPE STRING,END OF T_EKKO_STR. DATA: WA_P2RD_STZ_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_P2RD_STZ_STR-MANDT sy-vline
WA_P2RD_STZ_STR-DCT_PERNR sy-vline
WA_P2RD_STZ_STR-DCT_SEQNR sy-vline
WA_P2RD_STZ_STR-BEGVB sy-vline
WA_P2RD_STZ_STR-ANZVB sy-vline
WA_P2RD_STZ_STR-VBEZL sy-vline
WA_P2RD_STZ_STR-VBEZS sy-vline
WA_P2RD_STZ_STR-VBEZB sy-vline
WA_P2RD_STZ_STR-FBVBZ sy-vline
WA_P2RD_STZ_STR-AEBEJ sy-vline
WA_P2RD_STZ_STR-ENDVB sy-vline
WA_P2RD_STZ_STR-KJAHR sy-vline
WA_P2RD_STZ_STR-LGVBZ sy-vline
WA_P2RD_STZ_STR-BEGV2 sy-vline
WA_P2RD_STZ_STR-ANZV2 sy-vline
WA_P2RD_STZ_STR-VBE2L sy-vline
WA_P2RD_STZ_STR-VBE2S sy-vline
WA_P2RD_STZ_STR-VBEZ2 sy-vline
WA_P2RD_STZ_STR-FBVB2 sy-vline
WA_P2RD_STZ_STR-ENDV2 sy-vline
WA_P2RD_STZ_STR-KJAH2 sy-vline
WA_P2RD_STZ_STR-LGVB2 sy-vline
WA_P2RD_STZ_STR-BEGV3 sy-vline
WA_P2RD_STZ_STR-ANZV3 sy-vline
WA_P2RD_STZ_STR-VBE3L sy-vline
WA_P2RD_STZ_STR-VBE3S sy-vline
WA_P2RD_STZ_STR-VBEZ3 sy-vline
WA_P2RD_STZ_STR-FBVB3 sy-vline
WA_P2RD_STZ_STR-ENDV3 sy-vline
WA_P2RD_STZ_STR-KJAH3 sy-vline
WA_P2RD_STZ_STR-LGVB3 sy-vline
WA_P2RD_STZ_STR-BEGV4 sy-vline
WA_P2RD_STZ_STR-ANZV4 sy-vline
WA_P2RD_STZ_STR-VBE4L sy-vline
WA_P2RD_STZ_STR-VBE4S sy-vline
WA_P2RD_STZ_STR-VBEZ4 sy-vline
WA_P2RD_STZ_STR-FBVB4 sy-vline
WA_P2RD_STZ_STR-ENDV4 sy-vline
WA_P2RD_STZ_STR-KJAH4 sy-vline
WA_P2RD_STZ_STR-LGVB4 sy-vline
WA_P2RD_STZ_STR-BEGV5 sy-vline
WA_P2RD_STZ_STR-ANZV5 sy-vline
WA_P2RD_STZ_STR-VBE5L sy-vline
WA_P2RD_STZ_STR-VBE5S sy-vline
WA_P2RD_STZ_STR-VBEZ5 sy-vline
WA_P2RD_STZ_STR-FBVB5 sy-vline
WA_P2RD_STZ_STR-ENDV5 sy-vline
WA_P2RD_STZ_STR-KJAH5 sy-vline
WA_P2RD_STZ_STR-LGVB5 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.