Restriction for FOREACH loops
Within a FOREACH loop, the SELECT query must complete execution before any DELETE, INSERT, or UPDATE operation that changes the data set of the SELECT cursor. One way to ensure that the SELECT query completes, use an ORDER BY clause in the SELECT statement. The ORDER BY clause creates an index on the columns and prevents errors caused by UPDATE, INSERT, DELETE statements modifying the query results of the SELECT statement in the same FOREACH loop