ABAP Select data from SAP table FAGL_R_S_APAR_OBJ 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 FAGL_R_S_APAR_OBJ 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 FAGL_R_S_APAR_OBJ. 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 FAGL_R_S_APAR_OBJ 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_FAGL_R_S_APAR_OBJ TYPE STANDARD TABLE OF FAGL_R_S_APAR_OBJ,
      WA_FAGL_R_S_APAR_OBJ TYPE FAGL_R_S_APAR_OBJ,
      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: <FAGL_R_S_APAR_OBJ> TYPE FAGL_R_S_APAR_OBJ.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM FAGL_R_S_APAR_OBJ
*  INTO TABLE @DATA(IT_FAGL_R_S_APAR_OBJ2).
*--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_FAGL_R_S_APAR_OBJ INDEX 1 INTO DATA(WA_FAGL_R_S_APAR_OBJ2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_FAGL_R_S_APAR_OBJ ASSIGNING <FAGL_R_S_APAR_OBJ>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<FAGL_R_S_APAR_OBJ>-OBJ_INDEX = 1.
<FAGL_R_S_APAR_OBJ>-MANDT = 1.
<FAGL_R_S_APAR_OBJ>-RPLAN = 1.
<FAGL_R_S_APAR_OBJ>-KOART = 1.
<FAGL_R_S_APAR_OBJ>-BUKRS = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_FAGL_R_S_APAR_OBJ-GJAHR, sy-vline,
WA_FAGL_R_S_APAR_OBJ-BELNR, sy-vline,
WA_FAGL_R_S_APAR_OBJ-BUZEI, sy-vline,
WA_FAGL_R_S_APAR_OBJ-OBJ_TYPE_SUP, sy-vline,
WA_FAGL_R_S_APAR_OBJ-VAL1_SUP, sy-vline,
WA_FAGL_R_S_APAR_OBJ-VAL2_SUP, sy-vline.
ENDLOOP. *Add any further fields from structure WA_FAGL_R_S_APAR_OBJ 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_FAGL_R_S_APAR_OBJ 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_FAGL_R_S_APAR_OBJ INTO WA_FAGL_R_S_APAR_OBJ. *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 GJAHR, internal->external for field GJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-GJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-GJAHR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field BELNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-BELNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-BELNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PRCTR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-PRCTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-PRCTR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PPRCTR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-PPRCTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-PPRCTR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field SEGMENT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-SEGMENT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-SEGMENT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field PSEGMENT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-PSEGMENT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-PSEGMENT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field SEGMENT_NEW CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-SEGMENT_NEW IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-SEGMENT_NEW.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field HKONT CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-HKONT IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-HKONT.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field VBUND CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-VBUND IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-VBUND.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field REBZG CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-REBZG IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-REBZG.
WRITE:/ 'New Value:', ld_input.

*Conversion exit GJAHR, internal->external for field REBZJ CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-REBZJ IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-REBZJ.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field LIFNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-LIFNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-LIFNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit ALPHA, internal->external for field KUNNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-KUNNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-KUNNR.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AU132, internal->external for field WRBTR_BUZEI CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-WRBTR_BUZEI IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-WRBTR_BUZEI.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AU132, internal->external for field DMBTR_BUZEI CALL FUNCTION 'CONVERSION_EXIT_AU132_OUTPUT' EXPORTING input = WA_FAGL_R_S_APAR_OBJ-DMBTR_BUZEI IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGL_R_S_APAR_OBJ-DMBTR_BUZEI.
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_FAGL_R_S_APAR_OBJ_STR,
OBJ_INDEX TYPE STRING,
MANDT TYPE STRING,
RPLAN TYPE STRING,
KOART TYPE STRING,
BUKRS TYPE STRING,
GJAHR TYPE STRING,
BELNR TYPE STRING,
BUZEI TYPE STRING,
OBJ_TYPE_SUP TYPE STRING,
VAL1_SUP TYPE STRING,
VAL2_SUP TYPE STRING,
VAL3_SUP TYPE STRING,
SEQNR TYPE STRING,
MDATTR_OLD TYPE STRING,
MDATTR_NEW TYPE STRING,
PRCTR TYPE STRING,
PPRCTR TYPE STRING,
SEGMENT TYPE STRING,
PSEGMENT TYPE STRING,
SEGMENT_NEW TYPE STRING,
GSBER TYPE STRING,
BSTAT TYPE STRING,
UMSKS TYPE STRING,
HKONT TYPE STRING,
VBUND TYPE STRING,
BUDAT TYPE STRING,
CPUDT TYPE STRING,
REBZG TYPE STRING,
REBZJ TYPE STRING,
REBZZ TYPE STRING,
REBZT TYPE STRING,
LIFNR TYPE STRING,
KUNNR TYPE STRING,
WRBTR_BUZEI TYPE STRING,
WAERS_BUZEI TYPE STRING,
DMBTR_BUZEI TYPE STRING,
LDGRP TYPE STRING,
BWBER TYPE STRING,
GEN_TIM TYPE STRING,
GRP TYPE STRING,
XDEL TYPE STRING,
XCLEARED TYPE STRING,
STATUS TYPE STRING,
XLEVEL1 TYPE STRING,
XHAS_SKV TYPE STRING,
XSKV TYPE STRING,
XFC_VAL TYPE STRING,
XERROR TYPE STRING,
XGRP_LEAD TYPE STRING,
XNOT_UPD_ORI TYPE STRING,
XNO_RESIDUAL_ITEM TYPE STRING,
GRP1 TYPE STRING,END OF T_EKKO_STR. DATA: WA_FAGL_R_S_APAR_OBJ_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_FAGL_R_S_APAR_OBJ_STR-OBJ_INDEX sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-MANDT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-RPLAN sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-KOART sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-BUKRS sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-GJAHR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-BELNR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-BUZEI sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-OBJ_TYPE_SUP sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-VAL1_SUP sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-VAL2_SUP sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-VAL3_SUP sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-SEQNR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-MDATTR_OLD sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-MDATTR_NEW sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-PRCTR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-PPRCTR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-SEGMENT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-PSEGMENT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-SEGMENT_NEW sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-GSBER sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-BSTAT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-UMSKS sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-HKONT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-VBUND sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-BUDAT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-CPUDT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-REBZG sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-REBZJ sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-REBZZ sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-REBZT sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-LIFNR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-KUNNR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-WRBTR_BUZEI sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-WAERS_BUZEI sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-DMBTR_BUZEI sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-LDGRP sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-BWBER sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-GEN_TIM sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-GRP sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XDEL sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XCLEARED sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-STATUS sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XLEVEL1 sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XHAS_SKV sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XSKV sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XFC_VAL sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XERROR sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XGRP_LEAD sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XNOT_UPD_ORI sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-XNO_RESIDUAL_ITEM sy-vline
WA_FAGL_R_S_APAR_OBJ_STR-GRP1 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.