ABAP Select data from SAP table RHEAD0 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 RHEAD0 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 RHEAD0. 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 RHEAD0 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_RHEAD0 TYPE STANDARD TABLE OF RHEAD0,
      WA_RHEAD0 TYPE RHEAD0,
      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: <RHEAD0> TYPE RHEAD0.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM RHEAD0
*  INTO TABLE @DATA(IT_RHEAD02).
*--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_RHEAD0 INDEX 1 INTO DATA(WA_RHEAD02).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_RHEAD0 ASSIGNING <RHEAD0>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<RHEAD0>-PGNAME = 1.
<RHEAD0>-TIMEST = 1.
<RHEAD0>-COMP = 1.
<RHEAD0>-VERS = 1.
<RHEAD0>-FLAG1 = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_RHEAD0-FLAG2, sy-vline,
WA_RHEAD0-SSCR, sy-vline,
WA_RHEAD0-LIST, sy-vline,
WA_RHEAD0-FMBS, sy-vline,
WA_RHEAD0-FIDO, sy-vline,
WA_RHEAD0-FSEL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_RHEAD0 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_RHEAD0 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_RHEAD0 INTO WA_RHEAD0. *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_RHEAD0_STR,
PGNAME TYPE STRING,
TIMEST TYPE STRING,
COMP TYPE STRING,
VERS TYPE STRING,
FLAG1 TYPE STRING,
FLAG2 TYPE STRING,
SSCR TYPE STRING,
LIST TYPE STRING,
FMBS TYPE STRING,
FIDO TYPE STRING,
FSEL TYPE STRING,
FORMAT TYPE STRING,
LINC TYPE STRING,
LINS TYPE STRING,
EOPL TYPE STRING,
NSPH TYPE STRING,
AUTH TYPE STRING,
PAGC TYPE STRING,
FUNC TYPE STRING,
CLAS TYPE STRING,
ARIT TYPE STRING,
MSGI TYPE STRING,
LDBNAME TYPE STRING,
NTXT TYPE STRING,
UCCHECK TYPE STRING,
HASSEGMS TYPE STRING,
SUBC TYPE STRING,
SECU TYPE STRING,
SYSTXTYP TYPE STRING,
STORSEGLEN TYPE STRING,
STCOOFSG TYPE STRING,
CBLINIT TYPE STRING,
CBLTRIG TYPE STRING,
CBLBASE TYPE STRING,
CBLLITL TYPE STRING,
CBLSELC TYPE STRING,
CBLRSSCR TYPE STRING,
CBLDATA TYPE STRING,
CBLDATV TYPE STRING,
CBLCOMP TYPE STRING,
CBLDATL TYPE STRING,
CBLSTOR TYPE STRING,
CBLCONS TYPE STRING,
CBLSTXT TYPE STRING,
CBLTYPE TYPE STRING,
CBLPBAG TYPE STRING,
CBLDDNM TYPE STRING,
CBLCLAS TYPE STRING,
CBLINTI TYPE STRING,
CBLINTD TYPE STRING,
CBLINTC TYPE STRING,
CBLVTAB TYPE STRING,
CBLCREF TYPE STRING,
CBLOREF TYPE STRING,
CBLIREF TYPE STRING,
CBLEVNT TYPE STRING,
CBLGCIX TYPE STRING,
CBLINTR TYPE STRING,
CBLLITLIX TYPE STRING,
CBLSYDATA TYPE STRING,
CBLSYATTR TYPE STRING,
CBLSYTYPE TYPE STRING,
CBLSYIMETH TYPE STRING,
CBLSYIEVNT TYPE STRING,
CBLSYMETH TYPE STRING,
CBLSYEVNT TYPE STRING,
CBLSYIATTR TYPE STRING,
CBLSYIIMPL TYPE STRING,
CBLSYCLAS TYPE STRING,
CBLSYCOMP TYPE STRING,
CBLSYMEXCP TYPE STRING,
CBLSYMPARM TYPE STRING,
CBLTEXT TYPE STRING,
CBLEXCP TYPE STRING,
CBLTPLR TYPE STRING,
CBLSREF TYPE STRING,
CBLCLFR TYPE STRING,
CBEHEAD TYPE STRING,
CBECONT TYPE STRING,
CBEDATA TYPE STRING,
CBEDATL TYPE STRING,
CBEDATV TYPE STRING,
CBECOMP TYPE STRING,
CBETYPE TYPE STRING,
CBEPBAG TYPE STRING,
CBEDDNM TYPE STRING,
CBESTOR TYPE STRING,
CBEINIT TYPE STRING,
CBECONS TYPE STRING,
CBEBASE TYPE STRING,
CBETEXT TYPE STRING,
CBECLAS TYPE STRING,
CBEINTI TYPE STRING,
CBEINTD TYPE STRING,
CBEINTR TYPE STRING,
CBEINTC TYPE STRING,
CBEOREF TYPE STRING,
CBEIREF TYPE STRING,
CBECREF TYPE STRING,
CBEVTAB TYPE STRING,
CBEEVNT TYPE STRING,
CBEGCIX TYPE STRING,
CBESELC TYPE STRING,
CBERSSCR TYPE STRING,
CBELITL TYPE STRING,
CBELITLIX TYPE STRING,
CBETRIG TYPE STRING,
CBESYDATA TYPE STRING,
CBESYTYPE TYPE STRING,
CBESYCOMP TYPE STRING,
CBESYATTR TYPE STRING,
CBESYCLAS TYPE STRING,
CBESYIATTR TYPE STRING,
CBESYIIMPL TYPE STRING,
CBESYMETH TYPE STRING,
CBESYEVNT TYPE STRING,
CBESYIMETH TYPE STRING,
CBESYIEVNT TYPE STRING,
CBESYMEXCP TYPE STRING,
CBESYMPARM TYPE STRING,
CBESYTEXT TYPE STRING,
CBETPLR TYPE STRING,
CBEEXCP TYPE STRING,
CBESREF TYPE STRING,
CBECLFR TYPE STRING,
LOPTI TYPE STRING,
CBLCONT TYPE STRING,
CBLDATAMAX TYPE STRING,
CBLSTCO TYPE STRING,
CBESTCO TYPE STRING,
CBLSTIX TYPE STRING,
CBESTIX TYPE STRING,END OF T_EKKO_STR. DATA: WA_RHEAD0_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_RHEAD0_STR-PGNAME sy-vline
WA_RHEAD0_STR-TIMEST sy-vline
WA_RHEAD0_STR-COMP sy-vline
WA_RHEAD0_STR-VERS sy-vline
WA_RHEAD0_STR-FLAG1 sy-vline
WA_RHEAD0_STR-FLAG2 sy-vline
WA_RHEAD0_STR-SSCR sy-vline
WA_RHEAD0_STR-LIST sy-vline
WA_RHEAD0_STR-FMBS sy-vline
WA_RHEAD0_STR-FIDO sy-vline
WA_RHEAD0_STR-FSEL sy-vline
WA_RHEAD0_STR-FORMAT sy-vline
WA_RHEAD0_STR-LINC sy-vline
WA_RHEAD0_STR-LINS sy-vline
WA_RHEAD0_STR-EOPL sy-vline
WA_RHEAD0_STR-NSPH sy-vline
WA_RHEAD0_STR-AUTH sy-vline
WA_RHEAD0_STR-PAGC sy-vline
WA_RHEAD0_STR-FUNC sy-vline
WA_RHEAD0_STR-CLAS sy-vline
WA_RHEAD0_STR-ARIT sy-vline
WA_RHEAD0_STR-MSGI sy-vline
WA_RHEAD0_STR-LDBNAME sy-vline
WA_RHEAD0_STR-NTXT sy-vline
WA_RHEAD0_STR-UCCHECK sy-vline
WA_RHEAD0_STR-HASSEGMS sy-vline
WA_RHEAD0_STR-SUBC sy-vline
WA_RHEAD0_STR-SECU sy-vline
WA_RHEAD0_STR-SYSTXTYP sy-vline
WA_RHEAD0_STR-STORSEGLEN sy-vline
WA_RHEAD0_STR-STCOOFSG sy-vline
WA_RHEAD0_STR-CBLINIT sy-vline
WA_RHEAD0_STR-CBLTRIG sy-vline
WA_RHEAD0_STR-CBLBASE sy-vline
WA_RHEAD0_STR-CBLLITL sy-vline
WA_RHEAD0_STR-CBLSELC sy-vline
WA_RHEAD0_STR-CBLRSSCR sy-vline
WA_RHEAD0_STR-CBLDATA sy-vline
WA_RHEAD0_STR-CBLDATV sy-vline
WA_RHEAD0_STR-CBLCOMP sy-vline
WA_RHEAD0_STR-CBLDATL sy-vline
WA_RHEAD0_STR-CBLSTOR sy-vline
WA_RHEAD0_STR-CBLCONS sy-vline
WA_RHEAD0_STR-CBLSTXT sy-vline
WA_RHEAD0_STR-CBLTYPE sy-vline
WA_RHEAD0_STR-CBLPBAG sy-vline
WA_RHEAD0_STR-CBLDDNM sy-vline
WA_RHEAD0_STR-CBLCLAS sy-vline
WA_RHEAD0_STR-CBLINTI sy-vline
WA_RHEAD0_STR-CBLINTD sy-vline
WA_RHEAD0_STR-CBLINTC sy-vline
WA_RHEAD0_STR-CBLVTAB sy-vline
WA_RHEAD0_STR-CBLCREF sy-vline
WA_RHEAD0_STR-CBLOREF sy-vline
WA_RHEAD0_STR-CBLIREF sy-vline
WA_RHEAD0_STR-CBLEVNT sy-vline
WA_RHEAD0_STR-CBLGCIX sy-vline
WA_RHEAD0_STR-CBLINTR sy-vline
WA_RHEAD0_STR-CBLLITLIX sy-vline
WA_RHEAD0_STR-CBLSYDATA sy-vline
WA_RHEAD0_STR-CBLSYATTR sy-vline
WA_RHEAD0_STR-CBLSYTYPE sy-vline
WA_RHEAD0_STR-CBLSYIMETH sy-vline
WA_RHEAD0_STR-CBLSYIEVNT sy-vline
WA_RHEAD0_STR-CBLSYMETH sy-vline
WA_RHEAD0_STR-CBLSYEVNT sy-vline
WA_RHEAD0_STR-CBLSYIATTR sy-vline
WA_RHEAD0_STR-CBLSYIIMPL sy-vline
WA_RHEAD0_STR-CBLSYCLAS sy-vline
WA_RHEAD0_STR-CBLSYCOMP sy-vline
WA_RHEAD0_STR-CBLSYMEXCP sy-vline
WA_RHEAD0_STR-CBLSYMPARM sy-vline
WA_RHEAD0_STR-CBLTEXT sy-vline
WA_RHEAD0_STR-CBLEXCP sy-vline
WA_RHEAD0_STR-CBLTPLR sy-vline
WA_RHEAD0_STR-CBLSREF sy-vline
WA_RHEAD0_STR-CBLCLFR sy-vline
WA_RHEAD0_STR-CBEHEAD sy-vline
WA_RHEAD0_STR-CBECONT sy-vline
WA_RHEAD0_STR-CBEDATA sy-vline
WA_RHEAD0_STR-CBEDATL sy-vline
WA_RHEAD0_STR-CBEDATV sy-vline
WA_RHEAD0_STR-CBECOMP sy-vline
WA_RHEAD0_STR-CBETYPE sy-vline
WA_RHEAD0_STR-CBEPBAG sy-vline
WA_RHEAD0_STR-CBEDDNM sy-vline
WA_RHEAD0_STR-CBESTOR sy-vline
WA_RHEAD0_STR-CBEINIT sy-vline
WA_RHEAD0_STR-CBECONS sy-vline
WA_RHEAD0_STR-CBEBASE sy-vline
WA_RHEAD0_STR-CBETEXT sy-vline
WA_RHEAD0_STR-CBECLAS sy-vline
WA_RHEAD0_STR-CBEINTI sy-vline
WA_RHEAD0_STR-CBEINTD sy-vline
WA_RHEAD0_STR-CBEINTR sy-vline
WA_RHEAD0_STR-CBEINTC sy-vline
WA_RHEAD0_STR-CBEOREF sy-vline
WA_RHEAD0_STR-CBEIREF sy-vline
WA_RHEAD0_STR-CBECREF sy-vline
WA_RHEAD0_STR-CBEVTAB sy-vline
WA_RHEAD0_STR-CBEEVNT sy-vline
WA_RHEAD0_STR-CBEGCIX sy-vline
WA_RHEAD0_STR-CBESELC sy-vline
WA_RHEAD0_STR-CBERSSCR sy-vline
WA_RHEAD0_STR-CBELITL sy-vline
WA_RHEAD0_STR-CBELITLIX sy-vline
WA_RHEAD0_STR-CBETRIG sy-vline
WA_RHEAD0_STR-CBESYDATA sy-vline
WA_RHEAD0_STR-CBESYTYPE sy-vline
WA_RHEAD0_STR-CBESYCOMP sy-vline
WA_RHEAD0_STR-CBESYATTR sy-vline
WA_RHEAD0_STR-CBESYCLAS sy-vline
WA_RHEAD0_STR-CBESYIATTR sy-vline
WA_RHEAD0_STR-CBESYIIMPL sy-vline
WA_RHEAD0_STR-CBESYMETH sy-vline
WA_RHEAD0_STR-CBESYEVNT sy-vline
WA_RHEAD0_STR-CBESYIMETH sy-vline
WA_RHEAD0_STR-CBESYIEVNT sy-vline
WA_RHEAD0_STR-CBESYMEXCP sy-vline
WA_RHEAD0_STR-CBESYMPARM sy-vline
WA_RHEAD0_STR-CBESYTEXT sy-vline
WA_RHEAD0_STR-CBETPLR sy-vline
WA_RHEAD0_STR-CBEEXCP sy-vline
WA_RHEAD0_STR-CBESREF sy-vline
WA_RHEAD0_STR-CBECLFR sy-vline
WA_RHEAD0_STR-LOPTI sy-vline
WA_RHEAD0_STR-CBLCONT sy-vline
WA_RHEAD0_STR-CBLDATAMAX sy-vline
WA_RHEAD0_STR-CBLSTCO sy-vline
WA_RHEAD0_STR-CBESTCO sy-vline
WA_RHEAD0_STR-CBLSTIX sy-vline
WA_RHEAD0_STR-CBESTIX sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.