从PLSQL关联数组中选择?

时间:2010-10-18 17:55:57

标签: plsql

使用关联数组时是否可以使用SELECT FROM?我通过.NET应用程序将数组传递给存储过程,并且我希望能够在从另一个表中进行选择时将该数组用作条件。假设我正在向程序传递一系列ID,我希望能够这样做:

select * from table1 where userID in (select column_value from array)

数组的类型在包中定义:

type id_array is type of number index by pls_integer

3 个答案:

答案 0 :(得分:11)

是的,可以通过使用流水线函数包装数组来实现。这是关于流水线功能的一个很好的入门:

http://www.oracle-developer.net/display.php?id=429

更新:Oracle 12c现在支持使用TABLE运算符查询关联数组,只要在包规范中声明类型:https://galobalda.wordpress.com/2014/08/02/new-in-oracle-12c-querying-an-associative-array-in-plsql-programs/

e.g。

select * from table1
where userID in (select column_value from table(array));

答案 1 :(得分:1)

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

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

> 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

答案 2 :(得分:-1)

使用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;
/
相关问题