2

What is the best way to access table data from a SAP system?

I tried it with it RFC_READ_TABLE, but this RFC returns the data in concatenated form within a single column and has a size restriction for row data.

Is there a better way to access SAP data in generic form without creating custom RFCs into the system?

I am searching for a standard RFC solution, not a custom script.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111

3 Answers3

3

If I understand your question right, you want to read a table, but at time of programming, you don't know which table. With Select * from (tablename)you can read with a dynamic table name. The target field can be defined dynamic with create data.

An example (untested, currently I have no access to an SAP-system):

  DATA: lv_tablename TYPE string,
        ev_filelength TYPE i.

  lv_tablename = 'mara'. "e.g. a parameter

  DATA dref TYPE REF TO data.
  CREATE DATA dref TYPE TABLE OF (lv_tablename).

  FIELD-SYMBOLS: <wa> TYPE ANY TABLE.
  ASSIGN dref->* to <wa>.
  SELECT * FROM (lv_tablename) INTO TABLE <wa>. "Attention for test, may be large result
  "<wa> is like a variable with type table mara
Jagger
  • 10,350
  • 9
  • 51
  • 93
knut
  • 27,320
  • 6
  • 84
  • 112
  • Thanks for the example. But this is a custom script. Are there any other standard RFCs for this task? – Timo Westkämper Sep 16 '11 at 19:07
  • I thought you are inside the SAP-System, but you are outside and need the RFC. Ad hoc I have no other idea then RFC_READ_TABLE - sorry. – knut Sep 16 '11 at 19:22
  • Yep, unfortunately RFC_READ_TABLE is the only option. Even if it had a more generous row length limit, the basic approach is the only way to do it, as function module interfaces need to be statically defined. – Chris Carruthers Sep 19 '11 at 01:21
  • I am accepting this answer, because there obviously isn't a properly typed approach. Thanks. – Timo Westkämper Sep 19 '11 at 19:28
1
TYPES: BEGIN OF t_bseg,
*include structure bseg.
  bukrs     LIKE bseg-bukrs,
  belnr     LIKE bseg-belnr,
  gjahr     LIKE bseg-gjahr,
  buzei     LIKE bseg-buzei,
  mwskz     LIKE bseg-mwskz,         "Tax code
  umsks     LIKE bseg-umsks,         "Special G/L transaction type
  prctr     LIKE bseg-prctr,         "Profit Centre
  hkont     LIKE bseg-hkont,         "G/L account
  xauto     LIKE bseg-xauto,
  koart     LIKE bseg-koart,
  dmbtr     LIKE bseg-dmbtr,
  mwart     LIKE bseg-mwart,
  hwbas     LIKE bseg-hwbas,
  aufnr     LIKE bseg-aufnr,
  projk     LIKE bseg-projk,
  shkzg     LIKE bseg-shkzg,
  kokrs     LIKE bseg-kokrs,
 END OF t_bseg.
DATA: it_bseg TYPE STANDARD TABLE OF t_bseg INITIAL SIZE 0,
      wa_bseg TYPE t_bseg.

DATA: it_ekko TYPE STANDARD TABLE OF ekko.


*Select all fields of a SAP database table into in itab
SELECT *
  FROM ekko
  INTO TABLE it_ekko.
antyrat
  • 27,479
  • 9
  • 75
  • 76
saran
  • 11
  • 1
0

Try this snippet of RFC_READ_TABLE to get data in structured form:

  DATA: oref       TYPE REF TO cx_root,
        text       TYPE string,
        obj_data   TYPE REF TO data.
        lt_options TYPE TABLE OF rfc_db_opt,
        ls_option  TYPE rfc_db_opt,
        lt_fields  TYPE TABLE OF rfc_db_fld,
        ls_field   TYPE rfc_db_fld,
        lt_entries TYPE STANDARD TABLE OF tab512.

   FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE.

  TRY.

      ls_option-text = `some query`.
      APPEND ls_option TO lt_options.
      ls_field-fieldname = 'PARTNER'.
      APPEND ls_field TO lt_fields.
      ls_field-fieldname = 'TYPE'.
      APPEND ls_field TO lt_fields.
      ls_field-fieldname = 'BU_GROUP'.
      APPEND ls_field TO lt_fields.
      ls_field-fieldname = 'BU_SORT1'.
      APPEND ls_field TO lt_fields.
      ls_field-fieldname = 'TITLE'.
      APPEND ls_field TO lt_fields.

      CALL FUNCTION 'RFC_READ_TABLE' DESTINATION dest
        EXPORTING
          query_table = 'BUT000'
        TABLES
          options     = lt_options
          fields      = lt_fields
          data        = lt_entries.

    CATCH cx_root INTO oref.
      text = oref->get_text( ).
      MESSAGE text TYPE 'E'.
  ENDTRY.

  IF lt_entries IS NOT INITIAL.

    CREATE DATA obj_data TYPE TABLE OF but000.
    ASSIGN obj_data->* TO <fs_tab>.

    CREATE DATA obj_data TYPE but000.
    ASSIGN obj_data->* TO FIELD-SYMBOL(<fs_line>).

    LOOP AT lt_entries ASSIGNING FIELD-SYMBOL(<wa_data>).
        LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<fs_fld>).
          ASSIGN COMPONENT <fs_fld>-fieldname OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<lv_field>).
          IF <lv_field> IS ASSIGNED AND sy-subrc IS INITIAL.
            <lv_field> = <wa_data>-wa+<fs_fld>-offset(<fs_fld>-length).
          ENDIF.

          APPEND <fs_line> TO <fs_tab>.
        ENDLOOP.
    ENDLOOP.

  ENDIF.

  IF <fs_tab> IS NOT INITIAL.
    "Bingo!
  ENDIF.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90