ABAP Select data from SAP table PRHGR 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 PRHGR 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 PRHGR. 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 PRHGR 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_PRHGR TYPE STANDARD TABLE OF PRHGR,
      WA_PRHGR TYPE PRHGR,
      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: <PRHGR> TYPE PRHGR.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PRHGR
*  INTO TABLE @DATA(IT_PRHGR2).
*--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_PRHGR INDEX 1 INTO DATA(WA_PRHGR2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PRHGR ASSIGNING <PRHGR>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PRHGR>-TAB_INDEX = 1.
<PRHGR>-TAB_COUNT = 1.
<PRHGR>-WIN_TITLE = 1.
<PRHGR>-MON_FUNKT = 1.
<PRHGR>-MON_WEGID = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PRHGR-DEF_PLVAR, sy-vline,
WA_PRHGR-DEF_BEGDA, sy-vline,
WA_PRHGR-DEF_ENDDA, sy-vline,
WA_PRHGR-DEF_LIMIT, sy-vline,
WA_PRHGR-DEF_PROZT, sy-vline,
WA_PRHGR-DEF_ISTAT, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PRHGR 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_PRHGR 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_PRHGR INTO WA_PRHGR. *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_PRHGR_STR,
TAB_INDEX TYPE STRING,
TAB_COUNT TYPE STRING,
WIN_TITLE TYPE STRING,
MON_FUNKT TYPE STRING,
MON_WEGID TYPE STRING,
DEF_PLVAR TYPE STRING,
DEF_BEGDA TYPE STRING,
DEF_ENDDA TYPE STRING,
DEF_LIMIT TYPE STRING,
DEF_PROZT TYPE STRING,
DEF_ISTAT TYPE STRING,
DEF_PRIOX TYPE STRING,
REL_WEGID TYPE STRING,
REL_RSIGN TYPE STRING,
REL_RELAT TYPE STRING,
REL_RTEXT TYPE STRING,
INS_OTYPE TYPE STRING,
INS_OBJID TYPE STRING,
INS_RSIGN TYPE STRING,
INS_RELAT TYPE STRING,
INS_PRIOX TYPE STRING,
INS_BEGDA TYPE STRING,
INS_ENDDA TYPE STRING,
INS_SEQNR TYPE STRING,
INS_SCLAS TYPE STRING,
INS_SOBID TYPE STRING,
TXT_RELAT TYPE STRING,
TXT_OTYPE TYPE STRING,
TXT_OTYP1 TYPE STRING,
TXT_SHORT TYPE STRING,
TXT_STEXT TYPE STRING,
TXT_SHOR1 TYPE STRING,
TXT_STEX1 TYPE STRING,
GDATE TYPE STRING,END OF T_EKKO_STR. DATA: WA_PRHGR_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_PRHGR_STR-TAB_INDEX sy-vline
WA_PRHGR_STR-TAB_COUNT sy-vline
WA_PRHGR_STR-WIN_TITLE sy-vline
WA_PRHGR_STR-MON_FUNKT sy-vline
WA_PRHGR_STR-MON_WEGID sy-vline
WA_PRHGR_STR-DEF_PLVAR sy-vline
WA_PRHGR_STR-DEF_BEGDA sy-vline
WA_PRHGR_STR-DEF_ENDDA sy-vline
WA_PRHGR_STR-DEF_LIMIT sy-vline
WA_PRHGR_STR-DEF_PROZT sy-vline
WA_PRHGR_STR-DEF_ISTAT sy-vline
WA_PRHGR_STR-DEF_PRIOX sy-vline
WA_PRHGR_STR-REL_WEGID sy-vline
WA_PRHGR_STR-REL_RSIGN sy-vline
WA_PRHGR_STR-REL_RELAT sy-vline
WA_PRHGR_STR-REL_RTEXT sy-vline
WA_PRHGR_STR-INS_OTYPE sy-vline
WA_PRHGR_STR-INS_OBJID sy-vline
WA_PRHGR_STR-INS_RSIGN sy-vline
WA_PRHGR_STR-INS_RELAT sy-vline
WA_PRHGR_STR-INS_PRIOX sy-vline
WA_PRHGR_STR-INS_BEGDA sy-vline
WA_PRHGR_STR-INS_ENDDA sy-vline
WA_PRHGR_STR-INS_SEQNR sy-vline
WA_PRHGR_STR-INS_SCLAS sy-vline
WA_PRHGR_STR-INS_SOBID sy-vline
WA_PRHGR_STR-TXT_RELAT sy-vline
WA_PRHGR_STR-TXT_OTYPE sy-vline
WA_PRHGR_STR-TXT_OTYP1 sy-vline
WA_PRHGR_STR-TXT_SHORT sy-vline
WA_PRHGR_STR-TXT_STEXT sy-vline
WA_PRHGR_STR-TXT_SHOR1 sy-vline
WA_PRHGR_STR-TXT_STEX1 sy-vline
WA_PRHGR_STR-GDATE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.