ABAP Select data from SAP table P2RB_SV 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 P2RB_SV 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 P2RB_SV. 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 P2RB_SV 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_P2RB_SV TYPE STANDARD TABLE OF P2RB_SV,
      WA_P2RB_SV TYPE P2RB_SV,
      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: <P2RB_SV> TYPE P2RB_SV.

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

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

*Select data and declare internal table using in-line method @DATA
*SELECT *
*  FROM P2RB_SV
*  INTO TABLE @DATA(IT_P2RB_SV2).
*--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_P2RB_SV INDEX 1 INTO DATA(WA_P2RB_SV2).


*Demonstrate how to loop at an internal table and update values using a FIELD-SYMBOL
LOOP AT IT_P2RB_SV ASSIGNING <P2RB_SV>.
*To update a field value using a field symbol simply change the value via the field symbol pointer
<P2RB_SV>-MANDT = 1.
<P2RB_SV>-DCT_PERNR = 1.
<P2RB_SV>-DCT_SEQNR = 1.
<P2RB_SV>-DCT_ROWID = 1.
<P2RB_SV>-CNTR1 = 1.
ENDLOOP.

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

*Write selected data to screen/report before conversion.
  WRITE:/ sy-vline,   WA_P2RB_SV-CNTR2, sy-vline,
WA_P2RB_SV-BEGDA, sy-vline,
WA_P2RB_SV-ENDDA, sy-vline,
WA_P2RB_SV-ANSTA, sy-vline,
WA_P2RB_SV-JUPER, sy-vline,
WA_P2RB_SV-BUKRS, sy-vline.
ENDLOOP. *Add any further fields from structure WA_P2RB_SV 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_P2RB_SV 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_P2RB_SV INTO WA_P2RB_SV. *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 ISOLA, internal->external for field RSZLA CALL FUNCTION 'CONVERSION_EXIT_ISOLA_OUTPUT' EXPORTING input = WA_P2RB_SV-RSZLA IMPORTING output = ld_input.
WRITE:/ 'Org Value:', WA_P2RB_SV-RSZLA.
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_P2RB_SV_STR,
MANDT TYPE STRING,
DCT_PERNR TYPE STRING,
DCT_SEQNR TYPE STRING,
DCT_ROWID TYPE STRING,
CNTR1 TYPE STRING,
CNTR2 TYPE STRING,
BEGDA TYPE STRING,
ENDDA TYPE STRING,
ANSTA TYPE STRING,
JUPER TYPE STRING,
BUKRS TYPE STRING,
WERKS TYPE STRING,
BTRTL TYPE STRING,
PERSG TYPE STRING,
PERSK TYPE STRING,
PERID TYPE STRING,
ENTKM TYPE STRING,
WKWNG TYPE STRING,
TEILK TYPE STRING,
EMPCT TYPE STRING,
DYSCH TYPE STRING,
ZTERF TYPE STRING,
SCHKZ TYPE STRING,
WOSTD TYPE STRING,
MOSTD TYPE STRING,
ARBST TYPE STRING,
BSGRD TYPE STRING,
BESTE TYPE STRING,
BERGM TYPE STRING,
BEREG TYPE STRING,
CTTYP TYPE STRING,
ARBER TYPE STRING,
BEANC TYPE STRING,
BEAAN TYPE STRING,
BETIM TYPE STRING,
BEGWU TYPE STRING,
WAERS TYPE STRING,
BEUWG TYPE STRING,
BECAO TYPE STRING,
BEGPC TYPE STRING,
BECNR TYPE STRING,
BEMCH TYPE STRING,
BEGFB TYPE STRING,
BEGGH TYPE STRING,
BREGM TYPE STRING,
RSZBL TYPE STRING,
RSZGL TYPE STRING,
RSZKE TYPE STRING,
RSZNA TYPE STRING,
RSZPN TYPE STRING,
RSZRR TYPE STRING,
RSZVW TYPE STRING,
RSZCA TYPE STRING,
RSZFF TYPE STRING,
RSZFS TYPE STRING,
RSZKN TYPE STRING,
RSZLM TYPE STRING,
RSZNO TYPE STRING,
RSZSP TYPE STRING,
RSZWG TYPE STRING,
BVETL TYPE STRING,
BVFAM TYPE STRING,
BVEKI TYPE STRING,
FIXVA TYPE STRING,
PPAID TYPE STRING,
BEUW2 TYPE STRING,
REMES TYPE STRING,
EMPLP TYPE STRING,
APTYP TYPE STRING,
RETIR TYPE STRING,
MODRM TYPE STRING,
REDST TYPE STRING,
OSSIN TYPE STRING,
OREDD TYPE STRING,
ASSIN TYPE STRING,
CORIG TYPE STRING,
RFSCH TYPE STRING,
LOCUN TYPE STRING,
RSKAC TYPE STRING,
ALLCD TYPE STRING,
ALLST TYPE STRING,
BVCOA TYPE STRING,
MARBR TYPE STRING,
NACEC TYPE STRING,
OLDRR TYPE STRING,
EERST TYPE STRING,
RDCST TYPE STRING,
REMTY TYPE STRING,
CAPEX TYPE STRING,
DISPD TYPE STRING,
CFWPT TYPE STRING,
DISHW TYPE STRING,
RCAIN TYPE STRING,
STYPE TYPE STRING,
TMPFT TYPE STRING,
NMEDT TYPE STRING,
PSPCC TYPE STRING,
ILLDT TYPE STRING,
SEREX TYPE STRING,
CARME TYPE STRING,
SEXSD TYPE STRING,
TRASI TYPE STRING,
PERSM TYPE STRING,
SECDT TYPE STRING,
SUPCE TYPE STRING,
FLYCL TYPE STRING,
PSTWO TYPE STRING,
DENOD TYPE STRING,
ESWDS TYPE STRING,
ESMWH TYPE STRING,
RSZLA TYPE STRING,
REAME TYPE STRING,END OF T_EKKO_STR. DATA: WA_P2RB_SV_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_P2RB_SV_STR-MANDT sy-vline
WA_P2RB_SV_STR-DCT_PERNR sy-vline
WA_P2RB_SV_STR-DCT_SEQNR sy-vline
WA_P2RB_SV_STR-DCT_ROWID sy-vline
WA_P2RB_SV_STR-CNTR1 sy-vline
WA_P2RB_SV_STR-CNTR2 sy-vline
WA_P2RB_SV_STR-BEGDA sy-vline
WA_P2RB_SV_STR-ENDDA sy-vline
WA_P2RB_SV_STR-ANSTA sy-vline
WA_P2RB_SV_STR-JUPER sy-vline
WA_P2RB_SV_STR-BUKRS sy-vline
WA_P2RB_SV_STR-WERKS sy-vline
WA_P2RB_SV_STR-BTRTL sy-vline
WA_P2RB_SV_STR-PERSG sy-vline
WA_P2RB_SV_STR-PERSK sy-vline
WA_P2RB_SV_STR-PERID sy-vline
WA_P2RB_SV_STR-ENTKM sy-vline
WA_P2RB_SV_STR-WKWNG sy-vline
WA_P2RB_SV_STR-TEILK sy-vline
WA_P2RB_SV_STR-EMPCT sy-vline
WA_P2RB_SV_STR-DYSCH sy-vline
WA_P2RB_SV_STR-ZTERF sy-vline
WA_P2RB_SV_STR-SCHKZ sy-vline
WA_P2RB_SV_STR-WOSTD sy-vline
WA_P2RB_SV_STR-MOSTD sy-vline
WA_P2RB_SV_STR-ARBST sy-vline
WA_P2RB_SV_STR-BSGRD sy-vline
WA_P2RB_SV_STR-BESTE sy-vline
WA_P2RB_SV_STR-BERGM sy-vline
WA_P2RB_SV_STR-BEREG sy-vline
WA_P2RB_SV_STR-CTTYP sy-vline
WA_P2RB_SV_STR-ARBER sy-vline
WA_P2RB_SV_STR-BEANC sy-vline
WA_P2RB_SV_STR-BEAAN sy-vline
WA_P2RB_SV_STR-BETIM sy-vline
WA_P2RB_SV_STR-BEGWU sy-vline
WA_P2RB_SV_STR-WAERS sy-vline
WA_P2RB_SV_STR-BEUWG sy-vline
WA_P2RB_SV_STR-BECAO sy-vline
WA_P2RB_SV_STR-BEGPC sy-vline
WA_P2RB_SV_STR-BECNR sy-vline
WA_P2RB_SV_STR-BEMCH sy-vline
WA_P2RB_SV_STR-BEGFB sy-vline
WA_P2RB_SV_STR-BEGGH sy-vline
WA_P2RB_SV_STR-BREGM sy-vline
WA_P2RB_SV_STR-RSZBL sy-vline
WA_P2RB_SV_STR-RSZGL sy-vline
WA_P2RB_SV_STR-RSZKE sy-vline
WA_P2RB_SV_STR-RSZNA sy-vline
WA_P2RB_SV_STR-RSZPN sy-vline
WA_P2RB_SV_STR-RSZRR sy-vline
WA_P2RB_SV_STR-RSZVW sy-vline
WA_P2RB_SV_STR-RSZCA sy-vline
WA_P2RB_SV_STR-RSZFF sy-vline
WA_P2RB_SV_STR-RSZFS sy-vline
WA_P2RB_SV_STR-RSZKN sy-vline
WA_P2RB_SV_STR-RSZLM sy-vline
WA_P2RB_SV_STR-RSZNO sy-vline
WA_P2RB_SV_STR-RSZSP sy-vline
WA_P2RB_SV_STR-RSZWG sy-vline
WA_P2RB_SV_STR-BVETL sy-vline
WA_P2RB_SV_STR-BVFAM sy-vline
WA_P2RB_SV_STR-BVEKI sy-vline
WA_P2RB_SV_STR-FIXVA sy-vline
WA_P2RB_SV_STR-PPAID sy-vline
WA_P2RB_SV_STR-BEUW2 sy-vline
WA_P2RB_SV_STR-REMES sy-vline
WA_P2RB_SV_STR-EMPLP sy-vline
WA_P2RB_SV_STR-APTYP sy-vline
WA_P2RB_SV_STR-RETIR sy-vline
WA_P2RB_SV_STR-MODRM sy-vline
WA_P2RB_SV_STR-REDST sy-vline
WA_P2RB_SV_STR-OSSIN sy-vline
WA_P2RB_SV_STR-OREDD sy-vline
WA_P2RB_SV_STR-ASSIN sy-vline
WA_P2RB_SV_STR-CORIG sy-vline
WA_P2RB_SV_STR-RFSCH sy-vline
WA_P2RB_SV_STR-LOCUN sy-vline
WA_P2RB_SV_STR-RSKAC sy-vline
WA_P2RB_SV_STR-ALLCD sy-vline
WA_P2RB_SV_STR-ALLST sy-vline
WA_P2RB_SV_STR-BVCOA sy-vline
WA_P2RB_SV_STR-MARBR sy-vline
WA_P2RB_SV_STR-NACEC sy-vline
WA_P2RB_SV_STR-OLDRR sy-vline
WA_P2RB_SV_STR-EERST sy-vline
WA_P2RB_SV_STR-RDCST sy-vline
WA_P2RB_SV_STR-REMTY sy-vline
WA_P2RB_SV_STR-CAPEX sy-vline
WA_P2RB_SV_STR-DISPD sy-vline
WA_P2RB_SV_STR-CFWPT sy-vline
WA_P2RB_SV_STR-DISHW sy-vline
WA_P2RB_SV_STR-RCAIN sy-vline
WA_P2RB_SV_STR-STYPE sy-vline
WA_P2RB_SV_STR-TMPFT sy-vline
WA_P2RB_SV_STR-NMEDT sy-vline
WA_P2RB_SV_STR-PSPCC sy-vline
WA_P2RB_SV_STR-ILLDT sy-vline
WA_P2RB_SV_STR-SEREX sy-vline
WA_P2RB_SV_STR-CARME sy-vline
WA_P2RB_SV_STR-SEXSD sy-vline
WA_P2RB_SV_STR-TRASI sy-vline
WA_P2RB_SV_STR-PERSM sy-vline
WA_P2RB_SV_STR-SECDT sy-vline
WA_P2RB_SV_STR-SUPCE sy-vline
WA_P2RB_SV_STR-FLYCL sy-vline
WA_P2RB_SV_STR-PSTWO sy-vline
WA_P2RB_SV_STR-DENOD sy-vline
WA_P2RB_SV_STR-ESWDS sy-vline
WA_P2RB_SV_STR-ESMWH sy-vline
WA_P2RB_SV_STR-RSZLA sy-vline
WA_P2RB_SV_STR-REAME sy-vline INTO ld_text SEPARATED BY SPACE. *Add any further fields from structure WA_EKKO_STR you want to CONCATENATE... ENDLOOP. ENDFORM.