Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make massive selection SAP ABAP

I am doing a massive selection from database with the intention of saving it on application server or local directory. Since the db has loads of entries I first tried this way:

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. 

This resulted in a dump, with the following message:

Runtime Errors: DBIF_RSQL_INVALID_CURSOR
Exeption : CX_SY_OPEN_SQL_DB

I tried doing an alternative way as well:

 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.

This also resulted in a dump with the same message.

What is the best approach to this scenario?

like image 265
quefro Avatar asked Oct 20 '25 04:10

quefro


1 Answers

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. 

If your table has a compound primary key, i.e. multiple columns such as {MANDT, GJAHR, BELNR}, simply replace the types of the from and to fields with structures and adjust the column list in the first SELECT and the WHERE condition in the second SELECT appropriately.

like image 101
Florian Avatar answered Oct 21 '25 19:10

Florian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!