将任意rowtype作为参数传递

问题描述 投票:0回答:1

假设我有一个这样的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等?

oracle plsql
1个回答
0
投票

到目前为止,我可以理解您的要求,您希望将查询(SELECT table1.col, table2.col FROM TabA table1, TabB table2)中的所有记录传递给您的renderRec程序,并且您遇到的问题是在For Loop中运行查询的结果集时,您不知道应该将哪个dataype传递给程序,流程。当然其他人建议使用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;

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

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

- 处理一个Object表来保存多个结果集

Create or replace Type var_rec is table of rec ;
/

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

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;
/

- 匿名块将查询的值传递给过程

DECLARE
  v_recs var_rec:=var_rec();
BEGIN
  SELECT rec(table1.col,
             table2.col) 
  bulk collect  INTO v_recs
  FROM TabA table1,
    TabB table2 ;

  --Passing the resultset of the above query to the procedure.
  renderRec(v_recs);
END;
© www.soinside.com 2019 - 2024. All rights reserved.