ABAP Select data from SAP table VIRPRE2 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 VIRPRE2 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 VIRPRE2. 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 VIRPRE2 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_VIRPRE2 TYPE STANDARD TABLE OF VIRPRE2, WA_VIRPRE2 TYPE VIRPRE2, 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: <VIRPRE2> TYPE VIRPRE2. *Process all fields in table header/work area as string values PERFORM process_as_string_field_values CHANGING wa_VIRPRE2. SELECT * *restrict ABAP select to first 10 rows UP TO 10 ROWS FROM VIRPRE2 INTO TABLE IT_VIRPRE2. *Select data and declare internal table using in-line method @DATA *SELECT * * FROM VIRPRE2 * INTO TABLE @DATA(IT_VIRPRE22). *--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_VIRPRE2 INDEX 1 INTO DATA(WA_VIRPRE22). *Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL LOOP AT IT_VIRPRE2 ASSIGNING <VIRPRE2>.*To update a field value using a field symbol simply change the value via the field symbol pointer
<VIRPRE2>-MANDT = 1.
<VIRPRE2>-OBART = 1.
<VIRPRE2>-OBJNR = 1.
<VIRPRE2>-OBJNRX = 1.
<VIRPRE2>-DVONPER = 1.
ENDLOOP. LOOP AT IT_VIRPRE2 INTO WA_VIRPRE2. *Write horizonal line to screen report. WRITE:/ sy-uline. *Write selected data to screen/report before conversion. WRITE:/ sy-vline, WA_VIRPRE2-DBISPER, sy-vline,
WA_VIRPRE2-GJAHR, sy-vline,
WA_VIRPRE2-PERIO, sy-vline,
WA_VIRPRE2-PERDE, sy-vline,
WA_VIRPRE2-DSTICH, sy-vline,
WA_VIRPRE2-WAERS, sy-vline.
ENDLOOP. *Add any further fields from structure WA_VIRPRE2 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_VIRPRE2 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_VIRPRE2 INTO WA_VIRPRE2. *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 OBJEX, internal->external for field OBJNR CALL FUNCTION 'CONVERSION_EXIT_OBJEX_OUTPUT' EXPORTING input = WA_VIRPRE2-OBJNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-OBJNR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit VVOBJ, internal->external for field OBJNRX CALL FUNCTION 'CONVERSION_EXIT_VVOBJ_OUTPUT' EXPORTING input = WA_VIRPRE2-OBJNRX IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-OBJNRX.
WRITE:/ 'New Value:', ld_input.
*Conversion exit GJAHR, internal->external for field GJAHR CALL FUNCTION 'CONVERSION_EXIT_GJAHR_OUTPUT' EXPORTING input = WA_VIRPRE2-GJAHR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-GJAHR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit PERI7, internal->external for field PERIO CALL FUNCTION 'CONVERSION_EXIT_PERI7_OUTPUT' EXPORTING input = WA_VIRPRE2-PERIO IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-PERIO.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field KSTAR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-KSTAR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-KSTAR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit SWENR, internal->external for field SWENR CALL FUNCTION 'CONVERSION_EXIT_SWENR_OUTPUT' EXPORTING input = WA_VIRPRE2-SWENR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SWENR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit SGRNR, internal->external for field SGRNR CALL FUNCTION 'CONVERSION_EXIT_SGRNR_OUTPUT' EXPORTING input = WA_VIRPRE2-SGRNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SGRNR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit SGENR, internal->external for field SGENR CALL FUNCTION 'CONVERSION_EXIT_SGENR_OUTPUT' EXPORTING input = WA_VIRPRE2-SGENR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SGENR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit SMENR, internal->external for field SMENR CALL FUNCTION 'CONVERSION_EXIT_SMENR_OUTPUT' EXPORTING input = WA_VIRPRE2-SMENR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SMENR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field SMIVE CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-SMIVE IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SMIVE.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field SNKSL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-SNKSL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SNKSL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field SEMPSL CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-SEMPSL IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SEMPSL.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field SVWNR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-SVWNR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-SVWNR.
WRITE:/ 'New Value:', ld_input.
*Conversion exit CUNIT, internal->external for field MEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_VIRPRE2-MEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-MEINS.
WRITE:/ 'New Value:', ld_input.
*Conversion exit CUNIT, internal->external for field DUMMEINS CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' EXPORTING input = WA_VIRPRE2-DUMMEINS IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-DUMMEINS.
WRITE:/ 'New Value:', ld_input.
*Conversion exit BEKNZ, internal->external for field BEKNZ CALL FUNCTION 'CONVERSION_EXIT_BEKNZ_OUTPUT' EXPORTING input = WA_VIRPRE2-BEKNZ IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-BEKNZ.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field VERSN CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-VERSN IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-VERSN.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field WRTTP CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-WRTTP IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-WRTTP.
WRITE:/ 'New Value:', ld_input.
*Conversion exit ALPHA, internal->external for field PRCTR CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT' EXPORTING input = WA_VIRPRE2-PRCTR IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_VIRPRE2-PRCTR.
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_VIRPRE2_STR,
MANDT TYPE STRING,
OBART TYPE STRING,
OBJNR TYPE STRING,
OBJNRX TYPE STRING,
DVONPER TYPE STRING,
DBISPER TYPE STRING,
GJAHR TYPE STRING,
PERIO TYPE STRING,
PERDE TYPE STRING,
DSTICH TYPE STRING,
WAERS TYPE STRING,
KSTAR TYPE STRING,
WKGXXX2 TYPE STRING,
WTGXXX2 TYPE STRING,
WOGXXX2 TYPE STRING,
AEPERIO TYPE STRING,
WKGXXX2_A TYPE STRING,
WOGXXX2_A TYPE STRING,
WTGXXX2_A TYPE STRING,
WAERS_A TYPE STRING,
BUKRS TYPE STRING,
BUKVW TYPE STRING,
GSBER TYPE STRING,
SWENR TYPE STRING,
SGRNR TYPE STRING,
SGENR TYPE STRING,
SMENR TYPE STRING,
SMIVE TYPE STRING,
SNKSL TYPE STRING,
SEMPSL TYPE STRING,
SVWNR TYPE STRING,
SABRG TYPE STRING,
BABRE2 TYPE STRING,
MEINS TYPE STRING,
SABRGPROZ TYPE STRING,
DUMMEINS TYPE STRING,
VRGNG TYPE STRING,
BEKNZ TYPE STRING,
VERSN TYPE STRING,
KOKRS TYPE STRING,
STAGR TYPE STRING,
SMEXXX2 TYPE STRING,
SMAXXX2 TYPE STRING,
WRTTP TYPE STRING,
PRCTR TYPE STRING,END OF T_EKKO_STR. DATA: WA_VIRPRE2_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_VIRPRE2_STR-MANDT sy-vline
WA_VIRPRE2_STR-OBART sy-vline
WA_VIRPRE2_STR-OBJNR sy-vline
WA_VIRPRE2_STR-OBJNRX sy-vline
WA_VIRPRE2_STR-DVONPER sy-vline
WA_VIRPRE2_STR-DBISPER sy-vline
WA_VIRPRE2_STR-GJAHR sy-vline
WA_VIRPRE2_STR-PERIO sy-vline
WA_VIRPRE2_STR-PERDE sy-vline
WA_VIRPRE2_STR-DSTICH sy-vline
WA_VIRPRE2_STR-WAERS sy-vline
WA_VIRPRE2_STR-KSTAR sy-vline
WA_VIRPRE2_STR-WKGXXX2 sy-vline
WA_VIRPRE2_STR-WTGXXX2 sy-vline
WA_VIRPRE2_STR-WOGXXX2 sy-vline
WA_VIRPRE2_STR-AEPERIO sy-vline
WA_VIRPRE2_STR-WKGXXX2_A sy-vline
WA_VIRPRE2_STR-WOGXXX2_A sy-vline
WA_VIRPRE2_STR-WTGXXX2_A sy-vline
WA_VIRPRE2_STR-WAERS_A sy-vline
WA_VIRPRE2_STR-BUKRS sy-vline
WA_VIRPRE2_STR-BUKVW sy-vline
WA_VIRPRE2_STR-GSBER sy-vline
WA_VIRPRE2_STR-SWENR sy-vline
WA_VIRPRE2_STR-SGRNR sy-vline
WA_VIRPRE2_STR-SGENR sy-vline
WA_VIRPRE2_STR-SMENR sy-vline
WA_VIRPRE2_STR-SMIVE sy-vline
WA_VIRPRE2_STR-SNKSL sy-vline
WA_VIRPRE2_STR-SEMPSL sy-vline
WA_VIRPRE2_STR-SVWNR sy-vline
WA_VIRPRE2_STR-SABRG sy-vline
WA_VIRPRE2_STR-BABRE2 sy-vline
WA_VIRPRE2_STR-MEINS sy-vline
WA_VIRPRE2_STR-SABRGPROZ sy-vline
WA_VIRPRE2_STR-DUMMEINS sy-vline
WA_VIRPRE2_STR-VRGNG sy-vline
WA_VIRPRE2_STR-BEKNZ sy-vline
WA_VIRPRE2_STR-VERSN sy-vline
WA_VIRPRE2_STR-KOKRS sy-vline
WA_VIRPRE2_STR-STAGR sy-vline
WA_VIRPRE2_STR-SMEXXX2 sy-vline
WA_VIRPRE2_STR-SMAXXX2 sy-vline
WA_VIRPRE2_STR-WRTTP sy-vline
WA_VIRPRE2_STR-PRCTR sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.