需要在存储过程中返回一个临时结果集,这个结果集又是多个语句通过循环查询出来的,这时候就想到了将结果插入到临时表中,然后返回临时表的数据的思路,于是有了以下操作:
1.创建临时表
-- Create table
create global temporary table T_PROJECT_AMOUNT_TEMP
(project_id VARCHAR2(1000),con_amount_tax_s NUMBER
)
on commit preserve rows;
-- Add comments to the table
comment on table T_PROJECT_AMOUNT_TEMPis '临时表';
-- Add comments to the columns
comment on column T_PROJECT_AMOUNT_TEMP.project_idis '项目ID';
comment on column T_PROJECT_AMOUNT_TEMP.con_amount_tax_sis '项目合同金额';
2.创建存储过程
CREATE OR REPLACE PROCEDURE PRC_XXXX(TODO_CUR OUT PKG_QUERY.CUR_QUERY
) ASBEGINdelete from T_PROJECT_AMOUNT_TEMP;--遍历数据for dataOjb in (select * from xxx t where xxx) loop--往临时表插入记录insert into T_PROJECT_AMOUNT_TEMPselect xx,xx from XXX;insert into T_PROJECT_AMOUNT_TEMPselect xx,xx from XXX;END LOOP;--将临时表中的记录返回open TODO_CUR forselect * from T_PROJECT_AMOUNT_TEMP k;END PRC_XXXX;
在存储过程中先将临时表清空,然后再往临时表插入记录,不然数据会重复插入。