[英]Oracle - best way to return from a procedure an OUT SYS_REFCURSOR with all the rows and columns deleted from a table
I want to do something like this:我想做这样的事情:
create or replace procedure get_deleted_rows(result_set OUT SYS_REFCURSOR) is
begin
delete from table1
where start_date >= sysdate - 30 and start_date <= sysdate
returning * bulk collect into result_set;
end;
I saw that creating a type table of...
and using bulk collect
into a declared variable of this type was a way to go.我看到创建一个类型table of...
并将bulk collect
用于这种类型的声明变量是 go 的一种方法。 But, this table I'm deleting has several columns and doing this would generate a lot of complexity for me to place it in production.但是,我要删除的这个表有几列,这样做会给我带来很多复杂性,以便将其投入生产。
Is there a more simple way to return all the rows from a delete from a procedure?有没有更简单的方法从过程中的删除返回所有行?
... and doing this would generate a lot of complexity for me ...这样做会给我带来很多复杂性
I'm afraid that you'll just have to accept it.恐怕你只能接受它。
Here's an example of how to do it.这是一个如何做到这一点的例子。
Sample table;样品表; I'll delete rows for employees whose salaries are higher than 2000.我将删除工资高于 2000 的员工的行。
SQL> select * from test order by sal;
ENAME SAL STARS
---------- ---------- ----------
SMITH 800
JAMES 950
ADAMS 1100 *
WARD 1250 *
MARTIN 1250 *
TURNER 1500 *
ALLEN 1600 *
BLAKE 2850 ** --> delete Blake, Jones and Scott
JONES 2975 **
SCOTT 3000 ***
10 rows selected.
Let's start:开始吧:
SQL> create or replace type t_row is object
2 (ename varchar2(10), sal number, stars varchar2(10));
3 /
Type created.
SQL> create or replace type t_tab as table of t_row;
2 /
Type created.
Procedure:程序:
SQL> create or replace procedure p_test (par_rc out sys_refcursor)
2 is
3 l_tab t_tab;
4 begin
5 delete from test
6 where sal > 2000
7 returning t_row(ename, sal, stars) bulk collect into l_tab;
8
9 open par_Rc for select * from table (l_tab);
10 end;
11 /
Procedure created.
Testing:测试:
SQL> var l_rc refcursor
SQL>
SQL> exec p_test(:l_rc);
PL/SQL procedure successfully completed.
SQL> print l_rc
ENAME SAL STARS
---------- ---------- ----------
JONES 2975 ** --> Deleted, as expected
BLAKE 2850 **
SCOTT 3000 ***
What's left?还剩下什么?
SQL> select * from test order by sal;
ENAME SAL STARS
---------- ---------- ----------
SMITH 800
JAMES 950
ADAMS 1100 *
WARD 1250 *
MARTIN 1250 *
TURNER 1500 *
ALLEN 1600 *
7 rows selected.
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.