ABAP Select data from SAP table P41_YEAD_DEPTABLE 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 P41_YEAD_DEPTABLE 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 P41_YEAD_DEPTABLE. 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 P41_YEAD_DEPTABLE 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_P41_YEAD_DEPTABLE TYPE STANDARD TABLE OF P41_YEAD_DEPTABLE,
      WA_P41_YEAD_DEPTABLE TYPE P41_YEAD_DEPTABLE,
      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: <P41_YEAD_DEPTABLE> TYPE P41_YEAD_DEPTABLE.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM P41_YEAD_DEPTABLE
*  INTO TABLE @DATA(IT_P41_YEAD_DEPTABLE2).
*--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_P41_YEAD_DEPTABLE INDEX 1 INTO DATA(WA_P41_YEAD_DEPTABLE2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_P41_YEAD_DEPTABLE ASSIGNING <P41_YEAD_DEPTABLE>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<P41_YEAD_DEPTABLE>-RELAT01 = 1.
<P41_YEAD_DEPTABLE>-FOREI01 = 1.
<P41_YEAD_DEPTABLE>-FNAME01 = 1.
<P41_YEAD_DEPTABLE>-REGNO01 = 1.
<P41_YEAD_DEPTABLE>-DPTID01 = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_P41_YEAD_DEPTABLE-AGEID01, sy-vline,
WA_P41_YEAD_DEPTABLE-HNDID01, sy-vline,
WA_P41_YEAD_DEPTABLE-BACHD01, sy-vline,
WA_P41_YEAD_DEPTABLE-CHDID01, sy-vline,
WA_P41_YEAD_DEPTABLE-SINNA01, sy-vline,
WA_P41_YEAD_DEPTABLE-SINOT01, sy-vline.
ENDLOOP. *Add any further fields from structure WA_P41_YEAD_DEPTABLE 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_P41_YEAD_DEPTABLE 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_P41_YEAD_DEPTABLE INTO WA_P41_YEAD_DEPTABLE. *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 REGNO, internal->external for field REGNO01 CALL FUNCTION 'CONVERSION_EXIT_REGNO_OUTPUT' EXPORTING input = WA_P41_YEAD_DEPTABLE-REGNO01 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_P41_YEAD_DEPTABLE-REGNO01.
WRITE:/ 'New Value:', ld_input.

*Conversion exit REGNO, internal->external for field REGNO02 CALL FUNCTION 'CONVERSION_EXIT_REGNO_OUTPUT' EXPORTING input = WA_P41_YEAD_DEPTABLE-REGNO02 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_P41_YEAD_DEPTABLE-REGNO02.
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_P41_YEAD_DEPTABLE_STR,
RELAT01 TYPE STRING,
FOREI01 TYPE STRING,
FNAME01 TYPE STRING,
REGNO01 TYPE STRING,
DPTID01 TYPE STRING,
AGEID01 TYPE STRING,
HNDID01 TYPE STRING,
BACHD01 TYPE STRING,
CHDID01 TYPE STRING,
SINNA01 TYPE STRING,
SINOT01 TYPE STRING,
SIHNA01 TYPE STRING,
SIHOT01 TYPE STRING,
MEDNA01 TYPE STRING,
MEDOT01 TYPE STRING,
MFSNA01 TYPE STRING,
MFSOT01 TYPE STRING,
MHDNA01 TYPE STRING,
MHDOT01 TYPE STRING,
EDUNA01 TYPE STRING,
EDUOT01 TYPE STRING,
EHDNA01 TYPE STRING,
EHDOT01 TYPE STRING,
CRENA01 TYPE STRING,
CREOT01 TYPE STRING,
CASNA01 TYPE STRING,
DEBNA01 TYPE STRING,
DEBOT01 TYPE STRING,
BPFNA01 TYPE STRING,
BPFOT01 TYPE STRING,
TRDNA01 TYPE STRING,
TRDOT01 TYPE STRING,
TRANA01 TYPE STRING,
TRAOT01 TYPE STRING,
DONNA01 TYPE STRING,
DONOT01 TYPE STRING,
MPINA01 TYPE STRING,
MPIOT01 TYPE STRING,
ERONA01 TYPE STRING,
EROOT01 TYPE STRING,
RELAT02 TYPE STRING,
FOREI02 TYPE STRING,
FNAME02 TYPE STRING,
REGNO02 TYPE STRING,
DPTID02 TYPE STRING,
AGEID02 TYPE STRING,
HNDID02 TYPE STRING,
BACHD02 TYPE STRING,
CHDID02 TYPE STRING,
SINNA02 TYPE STRING,
SINOT02 TYPE STRING,
SIHNA02 TYPE STRING,
SIHOT02 TYPE STRING,
MEDNA02 TYPE STRING,
MEDOT02 TYPE STRING,
MFSNA02 TYPE STRING,
MFSOT02 TYPE STRING,
MHDNA02 TYPE STRING,
MHDOT02 TYPE STRING,
EDUNA02 TYPE STRING,
EDUOT02 TYPE STRING,
EHDNA02 TYPE STRING,
EHDOT02 TYPE STRING,
CRENA02 TYPE STRING,
CREOT02 TYPE STRING,
CASNA02 TYPE STRING,
DEBNA02 TYPE STRING,
DEBOT02 TYPE STRING,
BPFNA02 TYPE STRING,
BPFOT02 TYPE STRING,
TRDNA02 TYPE STRING,
TRDOT02 TYPE STRING,
TRANA02 TYPE STRING,
TRAOT02 TYPE STRING,
DONNA02 TYPE STRING,
DONOT02 TYPE STRING,
MPINA02 TYPE STRING,
MPIOT02 TYPE STRING,
ERONA02 TYPE STRING,
EROOT02 TYPE STRING,END OF T_EKKO_STR. DATA: WA_P41_YEAD_DEPTABLE_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_P41_YEAD_DEPTABLE_STR-RELAT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-FOREI01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-FNAME01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-REGNO01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DPTID01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-AGEID01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-HNDID01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-BACHD01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CHDID01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SINNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SINOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SIHNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SIHOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MEDNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MEDOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MFSNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MFSOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MHDNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MHDOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EDUNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EDUOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EHDNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EHDOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CRENA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CREOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CASNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DEBNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DEBOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-BPFNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-BPFOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRDNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRDOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRANA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRAOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DONNA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DONOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MPINA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MPIOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-ERONA01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EROOT01 sy-vline
WA_P41_YEAD_DEPTABLE_STR-RELAT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-FOREI02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-FNAME02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-REGNO02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DPTID02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-AGEID02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-HNDID02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-BACHD02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CHDID02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SINNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SINOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SIHNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-SIHOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MEDNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MEDOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MFSNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MFSOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MHDNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MHDOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EDUNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EDUOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EHDNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EHDOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CRENA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CREOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-CASNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DEBNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DEBOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-BPFNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-BPFOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRDNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRDOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRANA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-TRAOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DONNA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-DONOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MPINA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-MPIOT02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-ERONA02 sy-vline
WA_P41_YEAD_DEPTABLE_STR-EROOT02 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.