ABAP Select data from SAP table P1009 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 P1009 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 P1009. 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 P1009 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_P1009 TYPE STANDARD TABLE OF P1009,
      WA_P1009 TYPE P1009,
      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: <P1009> TYPE P1009.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM P1009
*  INTO TABLE @DATA(IT_P10092).
*--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_P1009 INDEX 1 INTO DATA(WA_P10092).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_P1009 ASSIGNING <P1009>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<P1009>-MANDT = 1.
<P1009>-PLVAR = 1.
<P1009>-OTYPE = 1.
<P1009>-OBJID = 1.
<P1009>-INFTY = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_P1009-SUBTY, sy-vline,
WA_P1009-ISTAT, sy-vline,
WA_P1009-PRIOX, sy-vline,
WA_P1009-BEGDA, sy-vline,
WA_P1009-ENDDA, sy-vline,
WA_P1009-VARYF, sy-vline.
ENDLOOP. *Add any further fields from structure WA_P1009 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_P1009 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_P1009 INTO WA_P1009. *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_P1009_STR,
MANDT TYPE STRING,
PLVAR TYPE STRING,
OTYPE TYPE STRING,
OBJID TYPE STRING,
INFTY TYPE STRING,
SUBTY TYPE STRING,
ISTAT TYPE STRING,
PRIOX TYPE STRING,
BEGDA TYPE STRING,
ENDDA TYPE STRING,
VARYF TYPE STRING,
SEQNR TYPE STRING,
AEDTM TYPE STRING,
UNAME TYPE STRING,
REASN TYPE STRING,
HISTO TYPE STRING,
ITXNR TYPE STRING,
UNART TYPE STRING,
UNART001 TYPE STRING,
UNART002 TYPE STRING,
UNART003 TYPE STRING,
UNART004 TYPE STRING,
UNART005 TYPE STRING,
UNART006 TYPE STRING,
UNART007 TYPE STRING,
UNART008 TYPE STRING,
UNART009 TYPE STRING,
UNART010 TYPE STRING,
UNART011 TYPE STRING,
UNART012 TYPE STRING,
UNART013 TYPE STRING,
UNART014 TYPE STRING,
UNART015 TYPE STRING,
UNART016 TYPE STRING,
UNART017 TYPE STRING,
UNART018 TYPE STRING,
UNART019 TYPE STRING,
UNART020 TYPE STRING,
UNART021 TYPE STRING,
UNART022 TYPE STRING,
UNART023 TYPE STRING,
UNART024 TYPE STRING,
UNART025 TYPE STRING,
UNART026 TYPE STRING,
UNART027 TYPE STRING,
UNART028 TYPE STRING,
UNART029 TYPE STRING,
UNART030 TYPE STRING,
UNART031 TYPE STRING,
UNART032 TYPE STRING,
UNART033 TYPE STRING,
UNART034 TYPE STRING,
UNART035 TYPE STRING,
UNART036 TYPE STRING,
UNART037 TYPE STRING,
UNART038 TYPE STRING,
UNART039 TYPE STRING,
UNART040 TYPE STRING,
UNART041 TYPE STRING,
UNART042 TYPE STRING,
UNART043 TYPE STRING,
UNART044 TYPE STRING,
UNART045 TYPE STRING,
UNART046 TYPE STRING,
UNART047 TYPE STRING,
UNART048 TYPE STRING,
UNART049 TYPE STRING,
UNART050 TYPE STRING,
UNART051 TYPE STRING,
UNART052 TYPE STRING,
UNART053 TYPE STRING,
UNART054 TYPE STRING,
UNART055 TYPE STRING,
UNART056 TYPE STRING,
UNART057 TYPE STRING,
UNART058 TYPE STRING,
UNART059 TYPE STRING,
UNART060 TYPE STRING,
UNART061 TYPE STRING,END OF T_EKKO_STR. DATA: WA_P1009_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_P1009_STR-MANDT sy-vline
WA_P1009_STR-PLVAR sy-vline
WA_P1009_STR-OTYPE sy-vline
WA_P1009_STR-OBJID sy-vline
WA_P1009_STR-INFTY sy-vline
WA_P1009_STR-SUBTY sy-vline
WA_P1009_STR-ISTAT sy-vline
WA_P1009_STR-PRIOX sy-vline
WA_P1009_STR-BEGDA sy-vline
WA_P1009_STR-ENDDA sy-vline
WA_P1009_STR-VARYF sy-vline
WA_P1009_STR-SEQNR sy-vline
WA_P1009_STR-AEDTM sy-vline
WA_P1009_STR-UNAME sy-vline
WA_P1009_STR-REASN sy-vline
WA_P1009_STR-HISTO sy-vline
WA_P1009_STR-ITXNR sy-vline
WA_P1009_STR-UNART sy-vline
WA_P1009_STR-UNART001 sy-vline
WA_P1009_STR-UNART002 sy-vline
WA_P1009_STR-UNART003 sy-vline
WA_P1009_STR-UNART004 sy-vline
WA_P1009_STR-UNART005 sy-vline
WA_P1009_STR-UNART006 sy-vline
WA_P1009_STR-UNART007 sy-vline
WA_P1009_STR-UNART008 sy-vline
WA_P1009_STR-UNART009 sy-vline
WA_P1009_STR-UNART010 sy-vline
WA_P1009_STR-UNART011 sy-vline
WA_P1009_STR-UNART012 sy-vline
WA_P1009_STR-UNART013 sy-vline
WA_P1009_STR-UNART014 sy-vline
WA_P1009_STR-UNART015 sy-vline
WA_P1009_STR-UNART016 sy-vline
WA_P1009_STR-UNART017 sy-vline
WA_P1009_STR-UNART018 sy-vline
WA_P1009_STR-UNART019 sy-vline
WA_P1009_STR-UNART020 sy-vline
WA_P1009_STR-UNART021 sy-vline
WA_P1009_STR-UNART022 sy-vline
WA_P1009_STR-UNART023 sy-vline
WA_P1009_STR-UNART024 sy-vline
WA_P1009_STR-UNART025 sy-vline
WA_P1009_STR-UNART026 sy-vline
WA_P1009_STR-UNART027 sy-vline
WA_P1009_STR-UNART028 sy-vline
WA_P1009_STR-UNART029 sy-vline
WA_P1009_STR-UNART030 sy-vline
WA_P1009_STR-UNART031 sy-vline
WA_P1009_STR-UNART032 sy-vline
WA_P1009_STR-UNART033 sy-vline
WA_P1009_STR-UNART034 sy-vline
WA_P1009_STR-UNART035 sy-vline
WA_P1009_STR-UNART036 sy-vline
WA_P1009_STR-UNART037 sy-vline
WA_P1009_STR-UNART038 sy-vline
WA_P1009_STR-UNART039 sy-vline
WA_P1009_STR-UNART040 sy-vline
WA_P1009_STR-UNART041 sy-vline
WA_P1009_STR-UNART042 sy-vline
WA_P1009_STR-UNART043 sy-vline
WA_P1009_STR-UNART044 sy-vline
WA_P1009_STR-UNART045 sy-vline
WA_P1009_STR-UNART046 sy-vline
WA_P1009_STR-UNART047 sy-vline
WA_P1009_STR-UNART048 sy-vline
WA_P1009_STR-UNART049 sy-vline
WA_P1009_STR-UNART050 sy-vline
WA_P1009_STR-UNART051 sy-vline
WA_P1009_STR-UNART052 sy-vline
WA_P1009_STR-UNART053 sy-vline
WA_P1009_STR-UNART054 sy-vline
WA_P1009_STR-UNART055 sy-vline
WA_P1009_STR-UNART056 sy-vline
WA_P1009_STR-UNART057 sy-vline
WA_P1009_STR-UNART058 sy-vline
WA_P1009_STR-UNART059 sy-vline
WA_P1009_STR-UNART060 sy-vline
WA_P1009_STR-UNART061 sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.