通用功能模块检索SAP表数据

时间:2011-09-16 17:14:32

标签: generics select dynamic sap abap

从SAP系统访问表数据的最佳方法是什么?

我用RFC_READ_TABLE尝试了它,但是这个RFC在单个列中以串联形式返回数据,并且对行数据有一个大小限制。

是否有更好的方法以通用形式访问SAP数据而无需在系统中创建自定义RFC?

我正在寻找标准的RFC解决方案,而不是自定义脚本。

4 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你想要阅读一个表,但在编程时,你不知道哪个表。 使用Select * from (tablename),您可以使用动态表名读取。 可以使用create data动态定义目标字段。

一个例子(未经测试,目前我无法访问SAP系统):

  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

答案 1 :(得分: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.

答案 2 :(得分:0)

尝试使用RFC_READ_TABLE的此片段以结构化形式获取数据:

  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.

答案 3 :(得分:0)

感谢您的帮助!

我只是认为这是一个小错误,也是我的建议: 错误:APPEND 。应该在ENDLOOP之后。 建议:使用FM BBP_RFC_READ_TABLE代替RFC_READ_TABLE。否则,您将遇到十进制数字的问题。