如何进行大量选择SAP ABAP

时间:2018-12-11 15:34:42

标签: sql select cursor sap abap

我正在从数据库中进行大量选择,目的是将其保存在应用程序服务器或本地目录中。 由于数据库有很多条目,因此我首先尝试这种方式:

SELECT * FROM db PACKAGE SIZE iv_package
    INTO CORRESPONDING FIELDS OF TABLE rt_data
    WHERE cond  IN so_cond
    AND   cond1 IN so_cond1.

SAVE(rt_data). 
ENDSELECT. 

这导致转储,并显示以下消息:

  

运行时错误:DBIF_RSQL_INVALID_CURSOR
  实例:CX_SY_OPEN_SQL_DB

我也尝试了另一种方法:

 OPEN CURSOR WITH HOLD s_cursor FOR
  SELECT * FROM db
    WHERE cond  IN so_cond
    AND   cond1 IN so_cond1.
  DO.
    FETCH NEXT CURSOR s_cursor INTO TABLE rt_data PACKAGE SIZE iv_package.

    SAVE(rt_data).
ENDDO.

这也导致转储带有相同的消息。

这种情况下最好的方法是什么?

1 个答案:

答案 0 :(得分:5)

TYPES:
  BEGIN OF key_package_type,
    from TYPE primary_key_type,
    to   TYPE primary_key_type,
  END OF key_package_type.
TYPES key_packages_type TYPE STANDARD TABLE OF key_package WITH EMPTY KEY.
DATA key_packages TYPE key_packages_type.

* select only the primary keys, in packages
SELECT primary_key_column FROM db
    INTO TABLE @DATA(key_package) PACKAGE SIZE package_size
    WHERE cond IN @condition AND cond1 IN other_condition
    ORDER BY primary_key_column.

  INSERT VALUE #( from = key_package[ 1 ]-primary_key_column
                  to   = key_package[ lines( key_package ) ]-primary_key_column )
    INTO TABLE key_packages.

ENDSELECT.

* select the actual data by the primary key packages
LOOP AT key_packages INTO key_package.
  SELECT * FROM db INTO TABLE @DATA(result_package)
    WHERE primary_key_column >= key_package-from
      AND primary_key_column <= key_package-to.
  save_to_file( result_package ).
ENDLOOP. 

如果您的表具有复合主键,即多列,例如{MANDT,GJAHR,BELNR},则只需用结构替换fromto字段的类型并调整列列表相应地,在第一个SELECT中选择WHERE条件。