ABAP Select data from SAP table PS0158 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 PS0158 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 PS0158. 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 PS0158 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_PS0158 TYPE STANDARD TABLE OF PS0158, WA_PS0158 TYPE PS0158, 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: <PS0158> TYPE PS0158. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_PS0158. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM PS0158 INTO TABLE IT_PS0158. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM PS0158 * INTO TABLE @DATA(IT_PS01582). *--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_PS0158 INDEX 1 INTO DATA(WA_PS01582). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_PS0158 ASSIGNING <PS0158>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<PS0158>-KALJAHR = 1.
<PS0158>-WTP01 = 1.
<PS0158>-AMT01 = 1.
<PS0158>-NUM01 = 1.
<PS0158>-RTE01 = 1.
ENDLOOP. LOOP AT IT_PS0158 INTO WA_PS0158. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_PS0158-WTP02, sy-vline,
WA_PS0158-AMT02, sy-vline,
WA_PS0158-NUM02, sy-vline,
WA_PS0158-RTE02, sy-vline,
WA_PS0158-WTP03, sy-vline,
WA_PS0158-AMT03, sy-vline.
ENDLOOP. *Add any further fields from structure WA_PS0158 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_PS0158 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_PS0158 INTO WA_PS0158. *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 GJAHR, internal->external for field KALJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_PS0158-KALJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_PS0158-KALJAHR.
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_PS0158_STR,
KALJAHR TYPE STRING,
WTP01 TYPE STRING,
AMT01 TYPE STRING,
NUM01 TYPE STRING,
RTE01 TYPE STRING,
WTP02 TYPE STRING,
AMT02 TYPE STRING,
NUM02 TYPE STRING,
RTE02 TYPE STRING,
WTP03 TYPE STRING,
AMT03 TYPE STRING,
NUM03 TYPE STRING,
RTE03 TYPE STRING,
WTP04 TYPE STRING,
AMT04 TYPE STRING,
NUM04 TYPE STRING,
RTE04 TYPE STRING,
WTP05 TYPE STRING,
AMT05 TYPE STRING,
NUM05 TYPE STRING,
RTE05 TYPE STRING,
WTP06 TYPE STRING,
AMT06 TYPE STRING,
NUM06 TYPE STRING,
RTE06 TYPE STRING,
WTP07 TYPE STRING,
AMT07 TYPE STRING,
NUM07 TYPE STRING,
RTE07 TYPE STRING,
WTP08 TYPE STRING,
AMT08 TYPE STRING,
NUM08 TYPE STRING,
RTE08 TYPE STRING,
WTP09 TYPE STRING,
AMT09 TYPE STRING,
NUM09 TYPE STRING,
RTE09 TYPE STRING,
WTP10 TYPE STRING,
AMT10 TYPE STRING,
NUM10 TYPE STRING,
RTE10 TYPE STRING,
WTP11 TYPE STRING,
AMT11 TYPE STRING,
NUM11 TYPE STRING,
RTE11 TYPE STRING,
WTP12 TYPE STRING,
AMT12 TYPE STRING,
NUM12 TYPE STRING,
RTE12 TYPE STRING,
WTP13 TYPE STRING,
AMT13 TYPE STRING,
NUM13 TYPE STRING,
RTE13 TYPE STRING,
WTP14 TYPE STRING,
AMT14 TYPE STRING,
NUM14 TYPE STRING,
RTE14 TYPE STRING,
WTP15 TYPE STRING,
AMT15 TYPE STRING,
NUM15 TYPE STRING,
RTE15 TYPE STRING,
WTP16 TYPE STRING,
AMT16 TYPE STRING,
NUM16 TYPE STRING,
RTE16 TYPE STRING,
WTP17 TYPE STRING,
AMT17 TYPE STRING,
NUM17 TYPE STRING,
RTE17 TYPE STRING,
WTP18 TYPE STRING,
AMT18 TYPE STRING,
NUM18 TYPE STRING,
RTE18 TYPE STRING,
WTP19 TYPE STRING,
AMT19 TYPE STRING,
NUM19 TYPE STRING,
RTE19 TYPE STRING,
WTP20 TYPE STRING,
AMT20 TYPE STRING,
NUM20 TYPE STRING,
RTE20 TYPE STRING,
ITWAE TYPE STRING,END OF T_EKKO_STR. DATA: WA_PS0158_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_PS0158_STR-KALJAHR sy-vline
WA_PS0158_STR-WTP01 sy-vline
WA_PS0158_STR-AMT01 sy-vline
WA_PS0158_STR-NUM01 sy-vline
WA_PS0158_STR-RTE01 sy-vline
WA_PS0158_STR-WTP02 sy-vline
WA_PS0158_STR-AMT02 sy-vline
WA_PS0158_STR-NUM02 sy-vline
WA_PS0158_STR-RTE02 sy-vline
WA_PS0158_STR-WTP03 sy-vline
WA_PS0158_STR-AMT03 sy-vline
WA_PS0158_STR-NUM03 sy-vline
WA_PS0158_STR-RTE03 sy-vline
WA_PS0158_STR-WTP04 sy-vline
WA_PS0158_STR-AMT04 sy-vline
WA_PS0158_STR-NUM04 sy-vline
WA_PS0158_STR-RTE04 sy-vline
WA_PS0158_STR-WTP05 sy-vline
WA_PS0158_STR-AMT05 sy-vline
WA_PS0158_STR-NUM05 sy-vline
WA_PS0158_STR-RTE05 sy-vline
WA_PS0158_STR-WTP06 sy-vline
WA_PS0158_STR-AMT06 sy-vline
WA_PS0158_STR-NUM06 sy-vline
WA_PS0158_STR-RTE06 sy-vline
WA_PS0158_STR-WTP07 sy-vline
WA_PS0158_STR-AMT07 sy-vline
WA_PS0158_STR-NUM07 sy-vline
WA_PS0158_STR-RTE07 sy-vline
WA_PS0158_STR-WTP08 sy-vline
WA_PS0158_STR-AMT08 sy-vline
WA_PS0158_STR-NUM08 sy-vline
WA_PS0158_STR-RTE08 sy-vline
WA_PS0158_STR-WTP09 sy-vline
WA_PS0158_STR-AMT09 sy-vline
WA_PS0158_STR-NUM09 sy-vline
WA_PS0158_STR-RTE09 sy-vline
WA_PS0158_STR-WTP10 sy-vline
WA_PS0158_STR-AMT10 sy-vline
WA_PS0158_STR-NUM10 sy-vline
WA_PS0158_STR-RTE10 sy-vline
WA_PS0158_STR-WTP11 sy-vline
WA_PS0158_STR-AMT11 sy-vline
WA_PS0158_STR-NUM11 sy-vline
WA_PS0158_STR-RTE11 sy-vline
WA_PS0158_STR-WTP12 sy-vline
WA_PS0158_STR-AMT12 sy-vline
WA_PS0158_STR-NUM12 sy-vline
WA_PS0158_STR-RTE12 sy-vline
WA_PS0158_STR-WTP13 sy-vline
WA_PS0158_STR-AMT13 sy-vline
WA_PS0158_STR-NUM13 sy-vline
WA_PS0158_STR-RTE13 sy-vline
WA_PS0158_STR-WTP14 sy-vline
WA_PS0158_STR-AMT14 sy-vline
WA_PS0158_STR-NUM14 sy-vline
WA_PS0158_STR-RTE14 sy-vline
WA_PS0158_STR-WTP15 sy-vline
WA_PS0158_STR-AMT15 sy-vline
WA_PS0158_STR-NUM15 sy-vline
WA_PS0158_STR-RTE15 sy-vline
WA_PS0158_STR-WTP16 sy-vline
WA_PS0158_STR-AMT16 sy-vline
WA_PS0158_STR-NUM16 sy-vline
WA_PS0158_STR-RTE16 sy-vline
WA_PS0158_STR-WTP17 sy-vline
WA_PS0158_STR-AMT17 sy-vline
WA_PS0158_STR-NUM17 sy-vline
WA_PS0158_STR-RTE17 sy-vline
WA_PS0158_STR-WTP18 sy-vline
WA_PS0158_STR-AMT18 sy-vline
WA_PS0158_STR-NUM18 sy-vline
WA_PS0158_STR-RTE18 sy-vline
WA_PS0158_STR-WTP19 sy-vline
WA_PS0158_STR-AMT19 sy-vline
WA_PS0158_STR-NUM19 sy-vline
WA_PS0158_STR-RTE19 sy-vline
WA_PS0158_STR-WTP20 sy-vline
WA_PS0158_STR-AMT20 sy-vline
WA_PS0158_STR-NUM20 sy-vline
WA_PS0158_STR-RTE20 sy-vline
WA_PS0158_STR-ITWAE sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.