从PLSQL关联数组中选择?

从PLSQL关联数组中选择?,plsql,Plsql,使用关联数组时是否可以使用“选择自”?我通过一个.NET应用程序将一个数组传递给一个存储过程,我希望能够在从另一个表中选择时使用该数组作为条件。假设我正在向过程传递一个ID数组,我希望能够做到这一点: 从表1中选择*,其中userID在select列中_value from array 数组的类型在包中定义: type id\u数组是pls\u整数的数字索引类型 不,您不能从PL/SQL数组中选择,因为您在select from语句中使用SQL,尽管您可以在SQL中使用DB定义的嵌套表类型

使用关联数组时是否可以使用“选择自”?我通过一个.NET应用程序将一个数组传递给一个存储过程,我希望能够在从另一个表中选择时使用该数组作为条件。假设我正在向过程传递一个ID数组,我希望能够做到这一点:

从表1中选择*,其中userID在select列中_value from array

数组的类型在包中定义:

type id\u数组是pls\u整数的数字索引类型

不,您不能从PL/SQL数组中选择,因为您在select from语句中使用SQL,尽管您可以在SQL中使用DB定义的嵌套表类型。可以帮助你开始

看看这个简单的合成示例:

> create type temp_t as table of int;/   
Type created.
> select 'test' from dual where 1 in (select * from table(temp_t(1,2,3)));

'TES
----
test

是的,这是可能的,通过使用流水线函数包装阵列。这里有一个很好的管道函数入门:

更新:Oracle 12c现在支持使用表运算符查询关联数组,只要在包规范中声明类型:

e、 g


使用PLSQL从嵌套表中进行选择的示例:

create type temp_r as OBJECT(
   temp_varchar2 varchar2(100),
   temp_number number(20)
   );
/

create type temp_t as TABLE of temp_r;
/   

set serveroutput on size 1000000
/

-- PLSQL starts here
declare
  temp_rec   temp_r := temp_r(null, null); -- empty constructor to initialize object
  temp_table temp_t := temp_t();           -- empty constructor to initialize object
  lv_ref_cursor     SYS_REFCURSOR; 

  lv_temp_varchar2 varchar(100);
  lv_temp_number   number(20);

begin
  temp_rec.temp_varchar2 := 'first';
  temp_rec.temp_number := 1;

  temp_table.extend;
  temp_table(1) := temp_rec;
  temp_table.extend;
  temp_table(2) := temp_r('second', 2);


     OPEN lv_ref_cursor FOR
        SELECT temp_varchar2, temp_number
        FROM   table(temp_table)
        where  temp_number = 1;

     fetch lv_ref_cursor into lv_temp_varchar2, lv_temp_number;
     close lv_ref_cursor;

  dbms_output.put_line('returns: ' || lv_temp_varchar2 || ', ' || lv_temp_number);

end;
/

所以根本就没有办法访问关联数组?是否仅用于传递数据?是否可以将关联数组强制转换为表?@ashtame不在SQL中不能使用关联数组,只允许使用嵌套表和varray。问题是我正在将关联数组从vb.net传递到存储过程。有没有办法将关联数组强制转换为表或嵌套表?@ashtame如果关联数组中的索引对您来说并不重要,那么您可以确保使用AA生成嵌套表:只需迭代数组并在PL/SQL函数中填充嵌套表集合
create type temp_r as OBJECT(
   temp_varchar2 varchar2(100),
   temp_number number(20)
   );
/

create type temp_t as TABLE of temp_r;
/   

set serveroutput on size 1000000
/

-- PLSQL starts here
declare
  temp_rec   temp_r := temp_r(null, null); -- empty constructor to initialize object
  temp_table temp_t := temp_t();           -- empty constructor to initialize object
  lv_ref_cursor     SYS_REFCURSOR; 

  lv_temp_varchar2 varchar(100);
  lv_temp_number   number(20);

begin
  temp_rec.temp_varchar2 := 'first';
  temp_rec.temp_number := 1;

  temp_table.extend;
  temp_table(1) := temp_rec;
  temp_table.extend;
  temp_table(2) := temp_r('second', 2);


     OPEN lv_ref_cursor FOR
        SELECT temp_varchar2, temp_number
        FROM   table(temp_table)
        where  temp_number = 1;

     fetch lv_ref_cursor into lv_temp_varchar2, lv_temp_number;
     close lv_ref_cursor;

  dbms_output.put_line('returns: ' || lv_temp_varchar2 || ', ' || lv_temp_number);

end;
/