将任意rowtype作为参数传递

时间:2018-03-06 16:07:39

标签: oracle plsql

假设我有一个这样的FOR循环:

FOR recs IN (SELECT table1.col, table2.col FROM TabA table1, TabB table2) 
LOOP
    renderRec(recs);
END LOOP;

我需要制作一个这样的程序:

PROCEDURE renderRec(inRec IN ????) AS ...

如何为renderRec()定义“recs”参数,因为它不像简单的表%rowtype等?

1 个答案:

答案 0 :(得分:0)

到目前为止,我可以理解您的要求,您希望将查询(SELECT table1.col, table2.col FROM TabA table1, TabB table2)中的所有记录传递到您的过程renderRec,并且您遇到的问题是在{{{} {}运行查询的结果集时1}},您不知道应该将哪个dataype传递给该过程。当然其他人建议使用For Loop这可能是一种做法。但我会通过另一种方式创建一个对象,然后将对象传递给refcursor。您将看到我在procedure使用BULK操作,这是最快速,最简单的方法。请参阅下文,并在内容中阅读我的评论:

- 表格设置

FOR LOOP

- 使用与查询结果集相同的列创建的对象以保存结果

create table tabA(col number);
/
insert into taba values(1);
insert into taba values(2);
insert into taba values(3);
/
create table tabB(col number);
/
insert into tabb values(11);
insert into tabb values(22);
insert into tabb values(33);

commit;

- 创建一个Object表来保存多个结果集

CREATE OR REPLACE Type rec is OBJECT
(
 col1 number,
 col2 number
);

-Procedure在1中获取查询的结果集并显示它。

Create or replace Type var_rec is table of rec ;
/

- 将查询值传递给过程

的无效块
CREATE OR REPLACE PROCEDURE renderRec(inpt IN var_rec)
AS
BEGIN
  FOR rec IN 1..inpt.count
  LOOP
    --displaying the resultset got from the select query
    dbms_output.put_line(inpt(rec).col1 ||'--'||inpt(rec).col2 );
  END LOOP;
END;
/