ORACLE 存储过程怎么返回临时表结果集

2024-12-19 02:58:04
推荐回答(4个)
回答1:

我刚做的,参考下:
SQL> CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
2 ID NUMBER,
3 ANAME VARCHAR2(20)
4 ) ON COMMIT DELETE ROWS;(也可以用PRESERVER ROWS,看实际需求)

Table created

SQL> create or replace procedure report_month_responsibility(
2 o_cur out sys_refcursor)
3 as
4 begin
5 insert into reprottest(id,aname) values(1,'1');
6 open o_cur for select * from reprottest;
7 end report_month_responsibility;
8 /

Procedure created

SQL> set serverout on
SQL> declare
2 v_id number;
3 v_aname varchar2(20);
4 o_cur sys_refcursor;
5 begin
6 report_month_responsibility(o_cur);
7 fetch o_cur into v_id,v_aname;
8 while o_cur%found loop
9 dbms_output.put_line('输出结果:'||v_id||','||v_aname);
10 fetch o_cur into v_id,v_aname;
11 end loop;
12 commit;
13 end;
14 /

输出结果:1,1

PL/SQL procedure successfully completed

你的那个ORA-01031: insufficient privileges,是权限不足的问题。
SQL代码:

CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID NUMBER,
ANAME VARCHAR2(20)
) ON COMMIT DELETE ROWS;

create or replace procedure report_month_responsibility(
o_cur out sys_refcursor)
as
begin
insert into reprottest(id,aname) values(1,'1');
open o_cur for select * from reprottest;
end report_month_responsibility;

declare
v_id number;
v_aname varchar2(20);
o_cur sys_refcursor;
begin
report_month_responsibility(o_cur);
fetch o_cur into v_id,v_aname;
while o_cur%found loop
dbms_output.put_line('输出结果:'||v_id||','||v_aname);
fetch o_cur into v_id,v_aname;
end loop;
commit;
end;
/

回答2:

STR:=' CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID INT,
ANAME VARCHAR2(20)
) ON COMMIT PRESERVE ROWS';

-------------------------------------------------------------------
这个是全局临时表,oracle下临时表在会话断时不会删除表定义的,仅是清掉记录罢啦(这个跟sql server是不同的).所以,你的大概可以改成这样:
CREATE OR REPLACE PROCEDURE Report_Month_Responsibility(
o_cur OUT SYS_REFCURSOR
)
IS
STR VARCHAR2(200);
tb_count INT;
BEGIN
--先判断全局临时表是否存在,没存在则重新建立:
select count(*) into tb_count from dba_tables where table_name='REPROTTEST';
if tb_count=0 then
STR:=' CREATE GLOBAL TEMPORARY TABLE REPROTTEST(
ID INT,
ANAME VARCHAR2(20)
) ON COMMIT PRESERVE ROWS';
execute immediate STR;
end if;

STR:='INSERT INTO REPROTTEST(ID,ANAME) VALUES(1,''1'')';
execute immediate STR;
COMMIT;
STR:='SELECT * FROM REPROTTEST';
OPEN o_cur FOR STR; -- 给游标变量赋值
END Report_Month_Responsibility;

/***********************************************************/
下面是调用存储过程并返回记录中每个字段值的调用示例:
declare
v_ID INT;
v_ANAME VARCHAR2(20);

--定义游标:
v_account_cur SYS_REFCURSOR;

begin
--调用存储过程:
Report_Month_Responsibility(v_account_cur);

fetch v_account_cur into v_ID,v_ANAME;
--用循环显示游标中的记录:
while v_account_cur%found loop
dbms_output.put_line('The value of column ID is: '||v_ID);--打引列ID
dbms_output.put_line('The value of column ANAME is: '||v_ANAME); --打引列ANAME
fetch v_account_cur into v_ID,v_ANAME;
end loop;
close v_account_cur;
execute immediate 'truncate TABLE REPROTTEST';
exception
when others then
dbms_output.put_line(sqlerrm);
end;

回答3:

ORA-01031: insufficient privileges
这个错是否你没有DBA权限或者是create any table的权限。
没用过临时表,不过永久表你这样写是OK的。

回答4:

为什么用临时表,能用sql搞定的就用sql搞定