PLSQL在数组中查找匹配值

时间:2014-05-26 11:51:47

标签: arrays oracle collections plsql oracle10g

我有一个数组,我将数组赋值为

TYPE array_p IS ARRAY (10000) OF VARCHAR2 (100);
my_array   array_p := array_p();

loop 
my_array.EXTEND;
my_array (my_array .LAST) :=
           'Product Id - '
        || product_Id
        || ', Product Item - '
        || product_Item
        || ', Amount -'
        || amt;
..... 

如何在迭代期间找到my_array中匹配的product_Idproduct_Item

1 个答案:

答案 0 :(得分:1)

我们可以使用MULTISET运算符来查询嵌套表集合。 Find out more

例如,此示例代码使用MEMBER OF来测试嵌套表中是否存在条目,使用IS A SET来测试唯一性。

declare
    my_array sys.dbms_debug_vc2coll := new sys.dbms_debug_vc2coll();
    str varchar2(4000);
begin
    for product in ( select '1' as id, 'iron' as item from dual union all
                     select '2' as id, 'gold' as item from dual union all
                     select '1' as id, 'iron' as item from dual union all
                     select '3' as id, 'redstone' as item from dual union all
                     select '4' as id, 'diamond' as item from dual 
               )
    loop
        str :=  'Product Id - '
                || product.Id
                || ', Product Item - '
                || product.Item;
        if str not member of my_array
        then
            my_array.extend();
            my_array(my_array.count()) := str;
        end if;
    end loop;    
    dbms_output.put_line('my_array count = '||to_char(my_array.count()));
    if my_array is a set then
        dbms_output.put_line('my_array has only unique entries ');    
    else
        dbms_output.put_line('my_array has duplicated ');  
    end if;
end;
/

对于记录,输出是......

my_array count = 4
my_array has only unique entries

PL/SQL procedure successfully completed.

SQL>

  

"我在调用SET时收到错误的参数数量。"

现在您已经包含了我已经意识到您的意思“阵列”的定义。特别是一般而言。 MULTISET运算符使用嵌套表但不使用VARRAY集合。这是VARRYAY的许多限制之一,也是开发人员很少使用VARRAY集合的原因之一。事实上,除非有一个铸铁要求来保持插入顺序,否则没有理由更喜欢它们而不是嵌套表。