I have the following sql (oracle) that removes all rows from a table except the 100 newest.
DELETE FROM my_table tab_outer
WHERE tab_outer.rowid IN (
-- Fetch rowids of rows to delete
SELECT rid FROM (
SELECT rownum r, rid FROM (
SELECT tab.rowid rid
FROM my_table tab
ORDER BY tab.created_date DESC
)
)
-- Delete everything but the 100 nesest rows
WHERE r > 100
)
-- Return newest date that was removed
RETURNING max(tab_outer.created_date) INTO :latestDate
This code sometimes gives a ORA-01422: exact fetch returns more than requested number of rows
claiming that more than one row was inserted into latestDate. How is this possible? The aggregate function (max) in the RETURNING INTO clause should ensure that only one row is returned, no? Could it have anything to do with the explicit use or rowid (I don't see how)?