日期:2014-05-17  浏览次数:20929 次

select结果集作为存储过程入参的问题
有一个存储过程excute_all(id),输入参数为id,
有一个表id_list,我想用select id from id_list的结果作为excue_all的入参,一次性执行多个id,
请问可否通过sql解决。
谢谢!

------解决方案--------------------
如果要用sql,只能把存储过程改为函数,那样就可以直接 select excute_all(id) from id_list

------解决方案--------------------
可以尝试用集合或游标做参数试试
另外,你可以把SELECT ID FROM TAB_NAME,放到存储过程里面用游标或集合来接收数据,然后进行相关的业务处理
------解决方案--------------------
第一步:建存储过程:

create or replace procedure myproc(mycur sys_refcursor)
as
        type myArrType is table of student%rowtype index by pls_integer;
        myArr myArrType;
begin
        fetch mycur bulk collect into myArr;
        for i in myArr.first..myArr.last loop
                dbms_output.put_line(myArr(i).name 
------解决方案--------------------
 ' : ' 
------解决方案--------------------
 myArr(i).score);
        end loop;
end;

第二步:建游标变量

SQL> var mycur refcursor;
SQL> begin
  2     open :mycur for select * from student;
  3  end;
  4  /

PL/SQL procedure successfully completed.

第三步:调用

SQL> exec myproc(:mycur);
a : 7
b : 11
c : 9
d : 30
e : 10
f : 33
g : 51
h : 39
i : 88
j : 46

PL/SQL procedure successfully completed.

SQL> 

------解决方案--------------------
ID如果很多的话,反复调用过程不死才怪呢,同时对数据库的负载也大。

如果他们的存储过程不好修改的话,你自己在外面封装一个过程吧,把你所需要的ID 插入到一个临时表中,存储过程读取这个表,在存储过程内部进行循环调用,在存储过程里面调,可以减少程序与数据库之间的IO。

从效率上讲,重新修改以前的过程是最好的