1

I use a SELECT statement to fill an internal table with a large amount of records. I am new to ABAP and OpenSQL. I know how cursors work and why I need them in this case, but I can't seem to find any good examples that show a correct implementation of them. This is the code I am working with:

TYPES: BEGIN OF lty_it_ids,
         iteration_id TYPE dat_itr_id,
       END OF lty_it_ids.

DATA:  lt_it_ids            TYPE STANDARD TABLE OF lty_it_ids,
       lt_records_to_delete TYPE STANDARD TABLE OF tab_01p.

SELECT 01r~iteration_id    
            INTO TABLE lt_it_ids
            FROM tab_01r AS 01r INNER JOIN tab_01a AS 01a
            ON 01r~iteration_id = 01a~iteration_id
            WHERE 01a~collection_id = i_collection_id.

IF lt_it_ids IS NOT INITIAL. 

  SELECT * FROM tab_01p INTO CORRESPONDING FIELDS OF TABLE lt_records_to_delete
           FOR ALL ENTRIES IN lt_it_ids
           WHERE iteration_id = lt_it_ids-iteration_id AND collection_id = i_collection_id.

  IF lt_records_to_delete IS NOT INITIAL.

    DELETE tab_01p FROM TABLE lt_records_to_delete.      

  ENDIF.
ENDIF.

In the first SELECT statement I fill a small internal table with some values that correspond with the index of a larger table. With these indexes I can search faster through the larger table to find all the entries I want to DELETE. It is the second SELECT statement that fills a large (a few million rows) internal table. All the records from this (lt_records_to_delete) internal table I want to delete from the database table.

In what way can I introduce a cursor to this code so it selects and deletes records in smaller batches?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Erik
  • 361
  • 5
  • 16

1 Answers1

1

There is a good example in the documentation. I am not entirely sure why you need to read the entries before deleting them, but there might be a good reason that you neglected to mention (for example logging the values). For the process you are implementing, be aware of the following warning in the documentation:

If write accesses are made on a database table for which a database cursor is open, the results set is database-specific and undefined. Avoid this kind of parallel access if possible.

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • I need to select them first because it's currently not possible to make a "delete for all entries" statement unfortunately. The example was very helpful indeed. I didn't end up doing it line per line as shown there, but with "package size". – Erik Jul 27 '17 at 13:32