ABAP Select data from SAP table FAGLPOSG 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 FAGLPOSG 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 FAGLPOSG. 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 FAGLPOSG 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_FAGLPOSG TYPE STANDARD TABLE OF FAGLPOSG,
      WA_FAGLPOSG TYPE FAGLPOSG,
      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: <FAGLPOSG> TYPE FAGLPOSG.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM FAGLPOSG
*  INTO TABLE @DATA(IT_FAGLPOSG2).
*--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_FAGLPOSG INDEX 1 INTO DATA(WA_FAGLPOSG2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_FAGLPOSG ASSIGNING <FAGLPOSG>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<FAGLPOSG>-AWSYS = 1.
<FAGLPOSG>-AWTYP = 1.
<FAGLPOSG>-BDIFF = 1.
<FAGLPOSG>-BDIF2 = 1.
<FAGLPOSG>-BDIF3 = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_FAGLPOSG-BEWAR, sy-vline,
WA_FAGLPOSG-DABRZ, sy-vline,
WA_FAGLPOSG-DMBTR, sy-vline,
WA_FAGLPOSG-DOCNR, sy-vline,
WA_FAGLPOSG-DOCLN, sy-vline,
WA_FAGLPOSG-FKBER, sy-vline.
ENDLOOP. *Add any further fields from structure WA_FAGLPOSG 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_FAGLPOSG 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_FAGLPOSG INTO WA_FAGLPOSG. *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 ALPHA, internal->external for field AWSYS CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_FAGLPOSG-AWSYS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-AWSYS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AC132, internal->external for field BDIFF CALL FUNCTION 'CONVERSION_EXIT_AC132_OUTPUT' EXPORTING input = WA_FAGLPOSG-BDIFF IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-BDIFF.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AC132, internal->external for field BDIF2 CALL FUNCTION 'CONVERSION_EXIT_AC132_OUTPUT' EXPORTING input = WA_FAGLPOSG-BDIF2 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-BDIF2.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AC132, internal->external for field BDIF3 CALL FUNCTION 'CONVERSION_EXIT_AC132_OUTPUT' EXPORTING input = WA_FAGLPOSG-BDIF3 IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-BDIF3.
WRITE:/ 'New Value:', ld_input.

*Conversion exit AC152, internal->external for field DMBTR CALL FUNCTION 'CONVERSION_EXIT_AC152_OUTPUT' EXPORTING input = WA_FAGLPOSG-DMBTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-DMBTR.
WRITE:/ 'New Value:', ld_input.

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

*Conversion exit IMKEY, internal->external for field IMKEY CALL FUNCTION 'CONVERSION_EXIT_IMKEY_OUTPUT' EXPORTING input = WA_FAGLPOSG-IMKEY IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-IMKEY.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit CUNIT, internal->external for field MEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_FAGLPOSG-MEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-MEINS.
WRITE:/ 'New Value:', ld_input.

*Conversion exit PERI7, internal->external for field PERIO CALL FUNCTION 'CONVERSION_EXIT_PERI7_OUTPUT' EXPORTING input = WA_FAGLPOSG-PERIO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-PERIO.
WRITE:/ 'New Value:', ld_input.

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

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

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

*Conversion exit ABPSP, internal->external for field PS_PSP_PNR CALL FUNCTION 'CONVERSION_EXIT_ABPSP_OUTPUT' EXPORTING input = WA_FAGLPOSG-PS_PSP_PNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-PS_PSP_PNR.
WRITE:/ 'New Value:', ld_input.

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

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

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

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

*Conversion exit AC152, internal->external for field WRBTR CALL FUNCTION 'CONVERSION_EXIT_AC152_OUTPUT' EXPORTING input = WA_FAGLPOSG-WRBTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_FAGLPOSG-WRBTR.
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_FAGLPOSG_STR,
AWSYS TYPE STRING,
AWTYP TYPE STRING,
BDIFF TYPE STRING,
BDIF2 TYPE STRING,
BDIF3 TYPE STRING,
BEWAR TYPE STRING,
DABRZ TYPE STRING,
DMBTR TYPE STRING,
DOCNR TYPE STRING,
DOCLN TYPE STRING,
FKBER TYPE STRING,
FKBER_LONG TYPE STRING,
GLVOR TYPE STRING,
IMKEY TYPE STRING,
KOKRS TYPE STRING,
KSTAR TYPE STRING,
KUNNR TYPE STRING,
LIFNR TYPE STRING,
MEINS TYPE STRING,
MENGE TYPE STRING,
PARGB TYPE STRING,
PERIO TYPE STRING,
PFKBER TYPE STRING,
PKOSTL TYPE STRING,
POPER TYPE STRING,
PPRCT TYPE STRING,
PSEGMENT TYPE STRING,
PS_PSP_PNR TYPE STRING,
RLDNR TYPE STRING,
RYEAR TYPE STRING,
SAKNR TYPE STRING,
SEGMENT TYPE STRING,
WRBTR TYPE STRING,
XOPVW TYPE STRING,
XLGCLR TYPE STRING,END OF T_EKKO_STR. DATA: WA_FAGLPOSG_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_FAGLPOSG_STR-AWSYS sy-vline
WA_FAGLPOSG_STR-AWTYP sy-vline
WA_FAGLPOSG_STR-BDIFF sy-vline
WA_FAGLPOSG_STR-BDIF2 sy-vline
WA_FAGLPOSG_STR-BDIF3 sy-vline
WA_FAGLPOSG_STR-BEWAR sy-vline
WA_FAGLPOSG_STR-DABRZ sy-vline
WA_FAGLPOSG_STR-DMBTR sy-vline
WA_FAGLPOSG_STR-DOCNR sy-vline
WA_FAGLPOSG_STR-DOCLN sy-vline
WA_FAGLPOSG_STR-FKBER sy-vline
WA_FAGLPOSG_STR-FKBER_LONG sy-vline
WA_FAGLPOSG_STR-GLVOR sy-vline
WA_FAGLPOSG_STR-IMKEY sy-vline
WA_FAGLPOSG_STR-KOKRS sy-vline
WA_FAGLPOSG_STR-KSTAR sy-vline
WA_FAGLPOSG_STR-KUNNR sy-vline
WA_FAGLPOSG_STR-LIFNR sy-vline
WA_FAGLPOSG_STR-MEINS sy-vline
WA_FAGLPOSG_STR-MENGE sy-vline
WA_FAGLPOSG_STR-PARGB sy-vline
WA_FAGLPOSG_STR-PERIO sy-vline
WA_FAGLPOSG_STR-PFKBER sy-vline
WA_FAGLPOSG_STR-PKOSTL sy-vline
WA_FAGLPOSG_STR-POPER sy-vline
WA_FAGLPOSG_STR-PPRCT sy-vline
WA_FAGLPOSG_STR-PSEGMENT sy-vline
WA_FAGLPOSG_STR-PS_PSP_PNR sy-vline
WA_FAGLPOSG_STR-RLDNR sy-vline
WA_FAGLPOSG_STR-RYEAR sy-vline
WA_FAGLPOSG_STR-SAKNR sy-vline
WA_FAGLPOSG_STR-SEGMENT sy-vline
WA_FAGLPOSG_STR-WRBTR sy-vline
WA_FAGLPOSG_STR-XOPVW sy-vline
WA_FAGLPOSG_STR-XLGCLR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.