ABAP Select data from SAP table PS0101 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 PS0101 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 PS0101. 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 PS0101 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_PS0101 TYPE STANDARD TABLE OF PS0101,
      WA_PS0101 TYPE PS0101,
      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: <PS0101> TYPE PS0101.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PS0101
*  INTO TABLE @DATA(IT_PS01012).
*--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_PS0101 INDEX 1 INTO DATA(WA_PS01012).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PS0101 ASSIGNING <PS0101>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PS0101>-BVKTL = 1.
<PS0101>-BVKKA = 1.
<PS0101>-BVAKA = 1.
<PS0101>-BVMKT = 1.
<PS0101>-BVETL = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PS0101-BVEKI, sy-vline,
WA_PS0101-BVECH, sy-vline,
WA_PS0101-BVEMV, sy-vline,
WA_PS0101-BVWMV, sy-vline,
WA_PS0101-BVWWE, sy-vline,
WA_PS0101-BVATL, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PS0101 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_PS0101 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_PS0101 INTO WA_PS0101. *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_PS0101_STR,
BVKTL TYPE STRING,
BVKKA TYPE STRING,
BVAKA TYPE STRING,
BVMKT TYPE STRING,
BVETL TYPE STRING,
BVEKI TYPE STRING,
BVECH TYPE STRING,
BVEMV TYPE STRING,
BVWMV TYPE STRING,
BVWWE TYPE STRING,
BVATL TYPE STRING,
BVAMT TYPE STRING,
BVFAM TYPE STRING,
BVTAX TYPE STRING,
BVVAK TYPE STRING,
BVFSV TYPE STRING,
WAERS TYPE STRING,
BVPCT TYPE STRING,
BVMUT TYPE STRING,
BVAMU TYPE STRING,
BVEPC TYPE STRING,
BVEXC TYPE STRING,
BVBOR TYPE STRING,
BVRES TYPE STRING,
BVFOR TYPE STRING,
BV281 TYPE STRING,
CHIAL TYPE STRING,
BVCOA TYPE STRING,
NR65S TYPE STRING,
N65SD TYPE STRING,
TAXDE TYPE STRING,END OF T_EKKO_STR. DATA: WA_PS0101_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_PS0101_STR-BVKTL sy-vline
WA_PS0101_STR-BVKKA sy-vline
WA_PS0101_STR-BVAKA sy-vline
WA_PS0101_STR-BVMKT sy-vline
WA_PS0101_STR-BVETL sy-vline
WA_PS0101_STR-BVEKI sy-vline
WA_PS0101_STR-BVECH sy-vline
WA_PS0101_STR-BVEMV sy-vline
WA_PS0101_STR-BVWMV sy-vline
WA_PS0101_STR-BVWWE sy-vline
WA_PS0101_STR-BVATL sy-vline
WA_PS0101_STR-BVAMT sy-vline
WA_PS0101_STR-BVFAM sy-vline
WA_PS0101_STR-BVTAX sy-vline
WA_PS0101_STR-BVVAK sy-vline
WA_PS0101_STR-BVFSV sy-vline
WA_PS0101_STR-WAERS sy-vline
WA_PS0101_STR-BVPCT sy-vline
WA_PS0101_STR-BVMUT sy-vline
WA_PS0101_STR-BVAMU sy-vline
WA_PS0101_STR-BVEPC sy-vline
WA_PS0101_STR-BVEXC sy-vline
WA_PS0101_STR-BVBOR sy-vline
WA_PS0101_STR-BVRES sy-vline
WA_PS0101_STR-BVFOR sy-vline
WA_PS0101_STR-BV281 sy-vline
WA_PS0101_STR-CHIAL sy-vline
WA_PS0101_STR-BVCOA sy-vline
WA_PS0101_STR-NR65S sy-vline
WA_PS0101_STR-N65SD sy-vline
WA_PS0101_STR-TAXDE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.