I have an Oracle procedure in which I would like to do the following:
- Copy records from one table to another
- Update original records to indicate they have been copied
Currently, I am using a FOR loop to process each record individually. This works, but it is a lot of code to do something I think could be simpler.
I would like to:
- Eliminate the for loop and copy the records with an INSERT INTO ... SELECT ... statement
- If copy is successful, update all selected records.
This may seem simple, just one INSERT and an UPDATE statement with the same WHERE clause. However, in the time between execution of the two statements, more records may be inserted that need to be copied. If I use the same WHERE clause, I may mark records as processed that have not actually been processed.
Is there a way I can save a list of the Primary Keys for use in both statements or merge the statements? Or would you recommend I stick with the FOR loop and process the records one at a time? All constructive input welcome.