ABAP Select data from SAP table PST03 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 PST03 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 PST03. 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 PST03 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_PST03 TYPE STANDARD TABLE OF PST03,
      WA_PST03 TYPE PST03,
      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: <PST03> TYPE PST03.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM PST03
*  INTO TABLE @DATA(IT_PST032).
*--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_PST03 INDEX 1 INTO DATA(WA_PST032).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_PST03 ASSIGNING <PST03>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<PST03>-SBART = 1.
<PST03>-SOART = 1.
<PST03>-SORTF = 1.
<PST03>-LZZRM = 1.
<PST03>-KNVFB = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_PST03-KNAEB, sy-vline,
WA_PST03-KNKJL, sy-vline,
WA_PST03-KNLJA, sy-vline,
WA_PST03-KNPAN, sy-vline,
WA_PST03-PPSTS, sy-vline,
WA_PST03-PBSTS, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PST03 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_PST03 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_PST03 INTO WA_PST03. *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_PST03_STR,
SBART TYPE STRING,
SOART TYPE STRING,
SORTF TYPE STRING,
LZZRM TYPE STRING,
KNVFB TYPE STRING,
KNAEB TYPE STRING,
KNKJL TYPE STRING,
KNLJA TYPE STRING,
KNPAN TYPE STRING,
PPSTS TYPE STRING,
PBSTS TYPE STRING,
LAVBZ TYPE STRING,
LALST TYPE STRING,
LAKST TYPE STRING,
LASOZ TYPE STRING,
LAVFB TYPE STRING,
LAAEB TYPE STRING,
LVBTR TYPE STRING,
LVVBZ TYPE STRING,
LVLST TYPE STRING,
LVKST TYPE STRING,
LVSOZ TYPE STRING,
LJBTR TYPE STRING,
LJVBZ TYPE STRING,
LJLST TYPE STRING,
LJKST TYPE STRING,
LJSOZ TYPE STRING,
LJVFB TYPE STRING,
LJAEB TYPE STRING,
BAVBZ TYPE STRING,
BALST TYPE STRING,
BAKST TYPE STRING,
BASOZ TYPE STRING,
BAVFB TYPE STRING,
BAAEB TYPE STRING,
BVBTR TYPE STRING,
AVBTR TYPE STRING,
BVVBZ TYPE STRING,
BVLST TYPE STRING,
BVKST TYPE STRING,
BVSOZ TYPE STRING,
BKBTR TYPE STRING,
AKBTR TYPE STRING,
BKVBZ TYPE STRING,
BKLST TYPE STRING,
BKKST TYPE STRING,
BKSOZ TYPE STRING,
BKVFB TYPE STRING,
BKAEB TYPE STRING,
BJBTR TYPE STRING,
BJVBZ TYPE STRING,
BJLST TYPE STRING,
BJKST TYPE STRING,
BJSOZ TYPE STRING,
BJVFB TYPE STRING,
BJAEB TYPE STRING,
BAKAP TYPE STRING,
BAMAL TYPE STRING,
BAMAS TYPE STRING,
BABAE TYPE STRING,
BASAL TYPE STRING,
BASVB TYPE STRING,
TABLZ TYPE STRING,
MKZVE TYPE STRING,
BASAV TYPE STRING,
KNZMJ TYPE STRING,
ALVPM TYPE STRING,
ZRE4V TYPE STRING,
ANPBT TYPE STRING,
VJAHR TYPE STRING,
ANZVB TYPE STRING,
VBEZL TYPE STRING,
VBEZS TYPE STRING,
STERB TYPE STRING,
VKAPA TYPE STRING,
STGFB TYPE STRING,
STGKU TYPE STRING,
VOPAM TYPE STRING,
TABFB TYPE STRING,
FVB TYPE STRING,
FVBZ TYPE STRING,
ALTE TYPE STRING,
SUMMJ TYPE STRING,
NGZVE TYPE STRING,
ZVE TYPE STRING,
ENTSCH TYPE STRING,
SONSTENT TYPE STRING,
JRE4ENT TYPE STRING,
VKVLZZ TYPE STRING,
VKVSONST TYPE STRING,
BKVKVLZZ TYPE STRING,
VFRB TYPE STRING,
VFRBS1 TYPE STRING,
VFRBS2 TYPE STRING,
WVFRB TYPE STRING,
WVFRBO TYPE STRING,
WVFRBM TYPE STRING,
VFRBLZZ TYPE STRING,
DBA_KLST TYPE STRING,
DBA_KSOL TYPE STRING,
JBMG TYPE STRING,
KFB TYPE STRING,
SOLZSZVE TYPE STRING,
SOLZSBMG TYPE STRING,
SOLZVBMG TYPE STRING,
SOLZMIN TYPE STRING,
LSTJAHR TYPE STRING,END OF T_EKKO_STR. DATA: WA_PST03_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_PST03_STR-SBART sy-vline
WA_PST03_STR-SOART sy-vline
WA_PST03_STR-SORTF sy-vline
WA_PST03_STR-LZZRM sy-vline
WA_PST03_STR-KNVFB sy-vline
WA_PST03_STR-KNAEB sy-vline
WA_PST03_STR-KNKJL sy-vline
WA_PST03_STR-KNLJA sy-vline
WA_PST03_STR-KNPAN sy-vline
WA_PST03_STR-PPSTS sy-vline
WA_PST03_STR-PBSTS sy-vline
WA_PST03_STR-LAVBZ sy-vline
WA_PST03_STR-LALST sy-vline
WA_PST03_STR-LAKST sy-vline
WA_PST03_STR-LASOZ sy-vline
WA_PST03_STR-LAVFB sy-vline
WA_PST03_STR-LAAEB sy-vline
WA_PST03_STR-LVBTR sy-vline
WA_PST03_STR-LVVBZ sy-vline
WA_PST03_STR-LVLST sy-vline
WA_PST03_STR-LVKST sy-vline
WA_PST03_STR-LVSOZ sy-vline
WA_PST03_STR-LJBTR sy-vline
WA_PST03_STR-LJVBZ sy-vline
WA_PST03_STR-LJLST sy-vline
WA_PST03_STR-LJKST sy-vline
WA_PST03_STR-LJSOZ sy-vline
WA_PST03_STR-LJVFB sy-vline
WA_PST03_STR-LJAEB sy-vline
WA_PST03_STR-BAVBZ sy-vline
WA_PST03_STR-BALST sy-vline
WA_PST03_STR-BAKST sy-vline
WA_PST03_STR-BASOZ sy-vline
WA_PST03_STR-BAVFB sy-vline
WA_PST03_STR-BAAEB sy-vline
WA_PST03_STR-BVBTR sy-vline
WA_PST03_STR-AVBTR sy-vline
WA_PST03_STR-BVVBZ sy-vline
WA_PST03_STR-BVLST sy-vline
WA_PST03_STR-BVKST sy-vline
WA_PST03_STR-BVSOZ sy-vline
WA_PST03_STR-BKBTR sy-vline
WA_PST03_STR-AKBTR sy-vline
WA_PST03_STR-BKVBZ sy-vline
WA_PST03_STR-BKLST sy-vline
WA_PST03_STR-BKKST sy-vline
WA_PST03_STR-BKSOZ sy-vline
WA_PST03_STR-BKVFB sy-vline
WA_PST03_STR-BKAEB sy-vline
WA_PST03_STR-BJBTR sy-vline
WA_PST03_STR-BJVBZ sy-vline
WA_PST03_STR-BJLST sy-vline
WA_PST03_STR-BJKST sy-vline
WA_PST03_STR-BJSOZ sy-vline
WA_PST03_STR-BJVFB sy-vline
WA_PST03_STR-BJAEB sy-vline
WA_PST03_STR-BAKAP sy-vline
WA_PST03_STR-BAMAL sy-vline
WA_PST03_STR-BAMAS sy-vline
WA_PST03_STR-BABAE sy-vline
WA_PST03_STR-BASAL sy-vline
WA_PST03_STR-BASVB sy-vline
WA_PST03_STR-TABLZ sy-vline
WA_PST03_STR-MKZVE sy-vline
WA_PST03_STR-BASAV sy-vline
WA_PST03_STR-KNZMJ sy-vline
WA_PST03_STR-ALVPM sy-vline
WA_PST03_STR-ZRE4V sy-vline
WA_PST03_STR-ANPBT sy-vline
WA_PST03_STR-VJAHR sy-vline
WA_PST03_STR-ANZVB sy-vline
WA_PST03_STR-VBEZL sy-vline
WA_PST03_STR-VBEZS sy-vline
WA_PST03_STR-STERB sy-vline
WA_PST03_STR-VKAPA sy-vline
WA_PST03_STR-STGFB sy-vline
WA_PST03_STR-STGKU sy-vline
WA_PST03_STR-VOPAM sy-vline
WA_PST03_STR-TABFB sy-vline
WA_PST03_STR-FVB sy-vline
WA_PST03_STR-FVBZ sy-vline
WA_PST03_STR-ALTE sy-vline
WA_PST03_STR-SUMMJ sy-vline
WA_PST03_STR-NGZVE sy-vline
WA_PST03_STR-ZVE sy-vline
WA_PST03_STR-ENTSCH sy-vline
WA_PST03_STR-SONSTENT sy-vline
WA_PST03_STR-JRE4ENT sy-vline
WA_PST03_STR-VKVLZZ sy-vline
WA_PST03_STR-VKVSONST sy-vline
WA_PST03_STR-BKVKVLZZ sy-vline
WA_PST03_STR-VFRB sy-vline
WA_PST03_STR-VFRBS1 sy-vline
WA_PST03_STR-VFRBS2 sy-vline
WA_PST03_STR-WVFRB sy-vline
WA_PST03_STR-WVFRBO sy-vline
WA_PST03_STR-WVFRBM sy-vline
WA_PST03_STR-VFRBLZZ sy-vline
WA_PST03_STR-DBA_KLST sy-vline
WA_PST03_STR-DBA_KSOL sy-vline
WA_PST03_STR-JBMG sy-vline
WA_PST03_STR-KFB sy-vline
WA_PST03_STR-SOLZSZVE sy-vline
WA_PST03_STR-SOLZSBMG sy-vline
WA_PST03_STR-SOLZVBMG sy-vline
WA_PST03_STR-SOLZMIN sy-vline
WA_PST03_STR-LSTJAHR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.