【问题标题】:Select from PLSQL Associative array?从 PLSQL 关联数组中选择?
【发布时间】:2011-04-27 02:08:59
【问题描述】:

在使用关联数组时可以使用 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

【问题讨论】:

    标签: plsql


    【解决方案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
    

    【讨论】:

    • 那么根本就没有办法访问关联数组吗?只是用来传递数据吗?
    • 是否可以将关联数组转换为表格?
    • @ashtame 不是你不能在 SQL 中使用关联数组,只允许嵌套表和可变数组。
    • 问题是我将关联数组从 vb.net 传递到存储过程。有没有办法将关联数组转换为表格或嵌套表格?
    • @ashtame 如果您的关联数组中的索引对您来说并不重要,那么您肯定可以使用 AA 创建一个嵌套表:只需遍历您的数组并在 PL/SQL 中填充一个嵌套表集合功能
    【解决方案2】:

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

    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/

    例如

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 1970-01-01
        • 2021-03-31
        • 2017-05-10
        • 1970-01-01
        • 2013-08-16
        • 2011-11-04
        相关资源
        最近更新 更多