ABAP Select data from SAP table PS0221 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 PS0221 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 PS0221. 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 PS0221 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_PS0221 TYPE STANDARD TABLE OF PS0221, WA_PS0221 TYPE PS0221, 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: <PS0221> TYPE PS0221. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_PS0221. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM PS0221 INTO TABLE IT_PS0221. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM PS0221 * INTO TABLE @DATA(IT_PS02212). *--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_PS0221 INDEX 1 INTO DATA(WA_PS02212). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_PS0221 ASSIGNING <PS0221>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<PS0221>-WAERS = 1.
<PS0221>-LGA01 = 1.
<PS0221>-TAX01 = 1.
<PS0221>-BWG01 = 1.
<PS0221>-STD01 = 1.
ENDLOOP. LOOP AT IT_PS0221 INTO WA_PS0221. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_PS0221-BET01, sy-vline,
WA_PS0221-OPK01, sy-vline,
WA_PS0221-LGA02, sy-vline,
WA_PS0221-TAX02, sy-vline,
WA_PS0221-BWG02, sy-vline,
WA_PS0221-STD02, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PS0221 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_PS0221 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_PS0221 INTO WA_PS0221. *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_PS0221_STR,
WAERS TYPE STRING,
LGA01 TYPE STRING,
TAX01 TYPE STRING,
BWG01 TYPE STRING,
STD01 TYPE STRING,
BET01 TYPE STRING,
OPK01 TYPE STRING,
LGA02 TYPE STRING,
TAX02 TYPE STRING,
BWG02 TYPE STRING,
STD02 TYPE STRING,
BET02 TYPE STRING,
OPK02 TYPE STRING,
LGA03 TYPE STRING,
TAX03 TYPE STRING,
BWG03 TYPE STRING,
STD03 TYPE STRING,
BET03 TYPE STRING,
OPK03 TYPE STRING,
LGA04 TYPE STRING,
TAX04 TYPE STRING,
BWG04 TYPE STRING,
STD04 TYPE STRING,
BET04 TYPE STRING,
OPK04 TYPE STRING,
LGA05 TYPE STRING,
TAX05 TYPE STRING,
BWG05 TYPE STRING,
STD05 TYPE STRING,
BET05 TYPE STRING,
OPK05 TYPE STRING,
LGA06 TYPE STRING,
TAX06 TYPE STRING,
BWG06 TYPE STRING,
STD06 TYPE STRING,
BET06 TYPE STRING,
OPK06 TYPE STRING,
LGA07 TYPE STRING,
TAX07 TYPE STRING,
BWG07 TYPE STRING,
STD07 TYPE STRING,
BET07 TYPE STRING,
OPK07 TYPE STRING,
LGA08 TYPE STRING,
TAX08 TYPE STRING,
BWG08 TYPE STRING,
STD08 TYPE STRING,
BET08 TYPE STRING,
OPK08 TYPE STRING,
LGA09 TYPE STRING,
TAX09 TYPE STRING,
BWG09 TYPE STRING,
STD09 TYPE STRING,
BET09 TYPE STRING,
OPK09 TYPE STRING,
LGA10 TYPE STRING,
TAX10 TYPE STRING,
BWG10 TYPE STRING,
STD10 TYPE STRING,
BET10 TYPE STRING,
OPK10 TYPE STRING,
LGA11 TYPE STRING,
TAX11 TYPE STRING,
BWG11 TYPE STRING,
STD11 TYPE STRING,
BET11 TYPE STRING,
OPK11 TYPE STRING,
LGA12 TYPE STRING,
TAX12 TYPE STRING,
BWG12 TYPE STRING,
STD12 TYPE STRING,
BET12 TYPE STRING,
OPK12 TYPE STRING,
LGA13 TYPE STRING,
TAX13 TYPE STRING,
BWG13 TYPE STRING,
STD13 TYPE STRING,
BET13 TYPE STRING,
OPK13 TYPE STRING,
LGA14 TYPE STRING,
TAX14 TYPE STRING,
BWG14 TYPE STRING,
STD14 TYPE STRING,
BET14 TYPE STRING,
OPK14 TYPE STRING,
LGA15 TYPE STRING,
TAX15 TYPE STRING,
BWG15 TYPE STRING,
STD15 TYPE STRING,
BET15 TYPE STRING,
OPK15 TYPE STRING,
LGA16 TYPE STRING,
TAX16 TYPE STRING,
BWG16 TYPE STRING,
STD16 TYPE STRING,
BET16 TYPE STRING,
OPK16 TYPE STRING,
LGA17 TYPE STRING,
TAX17 TYPE STRING,
BWG17 TYPE STRING,
STD17 TYPE STRING,
BET17 TYPE STRING,
OPK17 TYPE STRING,
LGA18 TYPE STRING,
TAX18 TYPE STRING,
BWG18 TYPE STRING,
STD18 TYPE STRING,
BET18 TYPE STRING,
OPK18 TYPE STRING,
LGA19 TYPE STRING,
TAX19 TYPE STRING,
BWG19 TYPE STRING,
STD19 TYPE STRING,
BET19 TYPE STRING,
OPK19 TYPE STRING,
LGA20 TYPE STRING,
TAX20 TYPE STRING,
BWG20 TYPE STRING,
STD20 TYPE STRING,
BET20 TYPE STRING,
OPK20 TYPE STRING,
PAYTY TYPE STRING,
PAYID TYPE STRING,
CHECN TYPE STRING,
OCRSN TYPE STRING,
TCP01 TYPE STRING,
TCP02 TYPE STRING,
TCP03 TYPE STRING,
TCP04 TYPE STRING,
TCP05 TYPE STRING,
TCP06 TYPE STRING,
TCP07 TYPE STRING,
TCP08 TYPE STRING,
TCP09 TYPE STRING,
TCP10 TYPE STRING,
TCP11 TYPE STRING,
TCP12 TYPE STRING,
TCP13 TYPE STRING,
TCP14 TYPE STRING,
TCP15 TYPE STRING,
TCP16 TYPE STRING,
TCP17 TYPE STRING,
TCP18 TYPE STRING,
TCP19 TYPE STRING,
TCP20 TYPE STRING,
WRKAR TYPE STRING,
RESAR TYPE STRING,
CBUSN TYPE STRING,
QCTXI TYPE STRING,
ADJRN TYPE STRING,
SEQNO TYPE STRING,
UOVRD TYPE STRING,
ROVRD TYPE STRING,
WOVRD TYPE STRING,END OF T_EKKO_STR. DATA: WA_PS0221_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_PS0221_STR-WAERS sy-vline
WA_PS0221_STR-LGA01 sy-vline
WA_PS0221_STR-TAX01 sy-vline
WA_PS0221_STR-BWG01 sy-vline
WA_PS0221_STR-STD01 sy-vline
WA_PS0221_STR-BET01 sy-vline
WA_PS0221_STR-OPK01 sy-vline
WA_PS0221_STR-LGA02 sy-vline
WA_PS0221_STR-TAX02 sy-vline
WA_PS0221_STR-BWG02 sy-vline
WA_PS0221_STR-STD02 sy-vline
WA_PS0221_STR-BET02 sy-vline
WA_PS0221_STR-OPK02 sy-vline
WA_PS0221_STR-LGA03 sy-vline
WA_PS0221_STR-TAX03 sy-vline
WA_PS0221_STR-BWG03 sy-vline
WA_PS0221_STR-STD03 sy-vline
WA_PS0221_STR-BET03 sy-vline
WA_PS0221_STR-OPK03 sy-vline
WA_PS0221_STR-LGA04 sy-vline
WA_PS0221_STR-TAX04 sy-vline
WA_PS0221_STR-BWG04 sy-vline
WA_PS0221_STR-STD04 sy-vline
WA_PS0221_STR-BET04 sy-vline
WA_PS0221_STR-OPK04 sy-vline
WA_PS0221_STR-LGA05 sy-vline
WA_PS0221_STR-TAX05 sy-vline
WA_PS0221_STR-BWG05 sy-vline
WA_PS0221_STR-STD05 sy-vline
WA_PS0221_STR-BET05 sy-vline
WA_PS0221_STR-OPK05 sy-vline
WA_PS0221_STR-LGA06 sy-vline
WA_PS0221_STR-TAX06 sy-vline
WA_PS0221_STR-BWG06 sy-vline
WA_PS0221_STR-STD06 sy-vline
WA_PS0221_STR-BET06 sy-vline
WA_PS0221_STR-OPK06 sy-vline
WA_PS0221_STR-LGA07 sy-vline
WA_PS0221_STR-TAX07 sy-vline
WA_PS0221_STR-BWG07 sy-vline
WA_PS0221_STR-STD07 sy-vline
WA_PS0221_STR-BET07 sy-vline
WA_PS0221_STR-OPK07 sy-vline
WA_PS0221_STR-LGA08 sy-vline
WA_PS0221_STR-TAX08 sy-vline
WA_PS0221_STR-BWG08 sy-vline
WA_PS0221_STR-STD08 sy-vline
WA_PS0221_STR-BET08 sy-vline
WA_PS0221_STR-OPK08 sy-vline
WA_PS0221_STR-LGA09 sy-vline
WA_PS0221_STR-TAX09 sy-vline
WA_PS0221_STR-BWG09 sy-vline
WA_PS0221_STR-STD09 sy-vline
WA_PS0221_STR-BET09 sy-vline
WA_PS0221_STR-OPK09 sy-vline
WA_PS0221_STR-LGA10 sy-vline
WA_PS0221_STR-TAX10 sy-vline
WA_PS0221_STR-BWG10 sy-vline
WA_PS0221_STR-STD10 sy-vline
WA_PS0221_STR-BET10 sy-vline
WA_PS0221_STR-OPK10 sy-vline
WA_PS0221_STR-LGA11 sy-vline
WA_PS0221_STR-TAX11 sy-vline
WA_PS0221_STR-BWG11 sy-vline
WA_PS0221_STR-STD11 sy-vline
WA_PS0221_STR-BET11 sy-vline
WA_PS0221_STR-OPK11 sy-vline
WA_PS0221_STR-LGA12 sy-vline
WA_PS0221_STR-TAX12 sy-vline
WA_PS0221_STR-BWG12 sy-vline
WA_PS0221_STR-STD12 sy-vline
WA_PS0221_STR-BET12 sy-vline
WA_PS0221_STR-OPK12 sy-vline
WA_PS0221_STR-LGA13 sy-vline
WA_PS0221_STR-TAX13 sy-vline
WA_PS0221_STR-BWG13 sy-vline
WA_PS0221_STR-STD13 sy-vline
WA_PS0221_STR-BET13 sy-vline
WA_PS0221_STR-OPK13 sy-vline
WA_PS0221_STR-LGA14 sy-vline
WA_PS0221_STR-TAX14 sy-vline
WA_PS0221_STR-BWG14 sy-vline
WA_PS0221_STR-STD14 sy-vline
WA_PS0221_STR-BET14 sy-vline
WA_PS0221_STR-OPK14 sy-vline
WA_PS0221_STR-LGA15 sy-vline
WA_PS0221_STR-TAX15 sy-vline
WA_PS0221_STR-BWG15 sy-vline
WA_PS0221_STR-STD15 sy-vline
WA_PS0221_STR-BET15 sy-vline
WA_PS0221_STR-OPK15 sy-vline
WA_PS0221_STR-LGA16 sy-vline
WA_PS0221_STR-TAX16 sy-vline
WA_PS0221_STR-BWG16 sy-vline
WA_PS0221_STR-STD16 sy-vline
WA_PS0221_STR-BET16 sy-vline
WA_PS0221_STR-OPK16 sy-vline
WA_PS0221_STR-LGA17 sy-vline
WA_PS0221_STR-TAX17 sy-vline
WA_PS0221_STR-BWG17 sy-vline
WA_PS0221_STR-STD17 sy-vline
WA_PS0221_STR-BET17 sy-vline
WA_PS0221_STR-OPK17 sy-vline
WA_PS0221_STR-LGA18 sy-vline
WA_PS0221_STR-TAX18 sy-vline
WA_PS0221_STR-BWG18 sy-vline
WA_PS0221_STR-STD18 sy-vline
WA_PS0221_STR-BET18 sy-vline
WA_PS0221_STR-OPK18 sy-vline
WA_PS0221_STR-LGA19 sy-vline
WA_PS0221_STR-TAX19 sy-vline
WA_PS0221_STR-BWG19 sy-vline
WA_PS0221_STR-STD19 sy-vline
WA_PS0221_STR-BET19 sy-vline
WA_PS0221_STR-OPK19 sy-vline
WA_PS0221_STR-LGA20 sy-vline
WA_PS0221_STR-TAX20 sy-vline
WA_PS0221_STR-BWG20 sy-vline
WA_PS0221_STR-STD20 sy-vline
WA_PS0221_STR-BET20 sy-vline
WA_PS0221_STR-OPK20 sy-vline
WA_PS0221_STR-PAYTY sy-vline
WA_PS0221_STR-PAYID sy-vline
WA_PS0221_STR-CHECN sy-vline
WA_PS0221_STR-OCRSN sy-vline
WA_PS0221_STR-TCP01 sy-vline
WA_PS0221_STR-TCP02 sy-vline
WA_PS0221_STR-TCP03 sy-vline
WA_PS0221_STR-TCP04 sy-vline
WA_PS0221_STR-TCP05 sy-vline
WA_PS0221_STR-TCP06 sy-vline
WA_PS0221_STR-TCP07 sy-vline
WA_PS0221_STR-TCP08 sy-vline
WA_PS0221_STR-TCP09 sy-vline
WA_PS0221_STR-TCP10 sy-vline
WA_PS0221_STR-TCP11 sy-vline
WA_PS0221_STR-TCP12 sy-vline
WA_PS0221_STR-TCP13 sy-vline
WA_PS0221_STR-TCP14 sy-vline
WA_PS0221_STR-TCP15 sy-vline
WA_PS0221_STR-TCP16 sy-vline
WA_PS0221_STR-TCP17 sy-vline
WA_PS0221_STR-TCP18 sy-vline
WA_PS0221_STR-TCP19 sy-vline
WA_PS0221_STR-TCP20 sy-vline
WA_PS0221_STR-WRKAR sy-vline
WA_PS0221_STR-RESAR sy-vline
WA_PS0221_STR-CBUSN sy-vline
WA_PS0221_STR-QCTXI sy-vline
WA_PS0221_STR-ADJRN sy-vline
WA_PS0221_STR-SEQNO sy-vline
WA_PS0221_STR-UOVRD sy-vline
WA_PS0221_STR-ROVRD sy-vline
WA_PS0221_STR-WOVRD sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.