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?