ORACLE-SQL:使用数组作为select参数的存储过程

时间:2013-10-02 10:08:40

标签: sql oracle stored-procedures plsql

我必须在我的存储过程中使用数组。 所以我创建了一个类型变量:

CREATE OR REPLACE TYPE integer_array is table of number;

然后我尝试编写我的存储过程,但我无法编译它:

create or replace
PROCEDURE SP_TEST(
      i_id_profiles in integer_array, 
      o_clients OUT SYS_REFCURSOR
   )
AS
BEGIN
open o_clients for 
    FOR i IN 1..i_id_profiles.count LOOP
      select a.id_client from b_client a, i_client_profile b where a.id_client = b.id_client 
      and b.id_profile = i_id_profiles(i);
    END LOOP;
END SP_TEST;
你能帮帮我吗?我想得到我选择的SYS_REFCURSOR。

感谢

错误:

  

PLS-00103:遇到符号" FOR"当期待其中一个   以下:( - + case mod new not null select with
   继续平均计数当前存在最大最小值   sql stddev sum variance执行forall合并时间戳   间隔日期管道      

PLS-00103:遇到符号"文件结束"当期待其中一个   以下内容:end not pragma final instantiable order overriding   静态成员构造函数映射

1 个答案:

答案 0 :(得分:3)

为查询(静态或动态)打开引用游标,无法打开for loop构造的引用游标或任何类型的循环构造。它只是在语义上不正确。 而且,在这种情况下,根本不需要任何类型的循环。当您创建integer_array作为sql类型(模式对象)时,您可以使用table运算符从该类型的实例中选择(将其表示为表)。所以你的程序可能如下所示:

create or replace PROCEDURE SP_TEST(
      i_id_profiles in integer_array, 
      o_clients OUT SYS_REFCURSOR
   )
AS
BEGIN
  open o_clients for 
      select a.id_client 
         from b_client a 
         join i_client_profile b 
           on (a.id_client = b.id_client)
         join table(i_id_profiles)  s
           on b.id_profile = s.column_value;
END SP_TEST;