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



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

我尝试了它的RFC_READ_TABLE,但这个RFC返回的数据在一个列内连接的形式,并有行数据的大小限制。

有没有更好的方法来访问SAP数据的通用形式,而不创建自定义rfc到系统中?

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

如果我对你的问题理解正确,你想读取一个表,但在编程时,你不知道是哪个表。使用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
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.

试试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.

相关内容

  • 没有找到相关文章

最新更新