You can return a cursor from a procedure or an anonymous block:
BEGIN
OPEN :cur FOR
SELECT *
FROM table
WHERE condition;
DELETE
FROM table
WHERE condition;
END;
The cursor will persist after delete.
See the entry in my blog for detailed explanations:
, and here is this entry in a nutshell:
CREATE TABLE t_deleter (id INT NOT NULL PRIMARY KEY, value VARCHAR2(50))
/
INSERT
INTO t_deleter (id, value)
VALUES (1, 'Value 1')
/
INSERT
INTO t_deleter (id, value)
VALUES (2, 'Value 2')
/
COMMIT
/
SELECT *
FROM t_deleter
/
VAR cur REFCURSOR
BEGIN
OPEN :cur FOR
SELECT *
FROM t_deleter
WHERE id = 1;
DELETE
FROM t_deleter
WHERE id = 1;
END;
/
PRINT cur
SELECT *
FROM t_deleter
/
Table created.
1 row created.
1 row created.
Commit complete.
ID VALUE
---------- --------------------------------------------------
1 Value 1
2 Value 2
PL/SQL procedure successfully completed.
/*
PRINT CUR
This is what returned to the client
*/
ID VALUE
---------- --------------------------------------------------
1 Value 1
/*
SELECT *
FROM t_deleter
This is what's left after the procedure completed
*/
ID VALUE
---------- --------------------------------------------------
2 Value 2