简体   繁体   English

如何在Oracle 10g中使用成员

[英]How to use member of in oracle 10g

I want to check if an element is a member of sys.odcinumberlist varray. 我想检查一个元素是否是sys.odcinumberlist varray的成员。

But it's not working... 但这不起作用...

What's wrong with my code? 我的代码有什么问题?

DECLARE
  v_list sys.Odcinumberlist := sys.Odcinumberlist( 1, 2, 3, 4 );
BEGIN

  FOR i IN 1..v_list.COUNT LOOP
    dbms_output.put_line( v_list(i) );
  END LOOP;

  IF 1 MEMBER OF v_list THEN
    dbms_output.put_line( 'yes' );
  ELSE
    dbms_output.put_line( 'no' );    
  END IF;

END;

The error this code throws is 该代码引发的错误是

ORA-06550: line 9, column 6:
PLS-00306: wrong number or types of arguments in call to 'MEMBER OF'
ORA-06550: line 9, column 3:
PL/SQL: Statement ignored

SYS.Odcinumberlist is VARRAY and comparison of nested tables is not supported for VARRAYS. SYS.Odcinumberlist为VARRAY,VARRAYS不支持嵌套表的比较。 More info here --> https://docs.oracle.com/cd/B12037_01/appdev.101/b10799/adobjcol.htm 此处更多信息-> https://docs.oracle.com/cd/B12037_01/appdev.101/b10799/adobjcol.htm
You can use SQL and the table function for this. 您可以为此使用SQL和表函数。 See the example below 参见下面的例子

DECLARE
  v_list sys.Odcinumberlist := sys.Odcinumberlist( 1, 2, 3, 4 );
  v_member_found char(3);
BEGIN

  FOR i IN 1..v_list.COUNT LOOP
    dbms_output.put_line( v_list(i) );
  END LOOP;

  begin
   select 'yes' into v_member_found 
    from table(v_list) 
   where column_value = 1;
  exception
    when no_data_found 
     then v_member_found := 'no';
  end;
  dbms_output.put_line(v_member_found);

END;

Alternatively, you can loop over the collection 或者,您可以遍历集合

DECLARE
  v_list sys.Odcinumberlist := sys.Odcinumberlist( 1, 2, 3, 4 );
  v_member_found boolean := false;
BEGIN

  FOR i IN 1..v_list.COUNT LOOP
    dbms_output.put_line( v_list(i) );
  END LOOP;


  For i in 1..v_list.COUNT LOOP <<SearchLoop>>
    if v_list(i) = 1 then
       dbms_output.put_line('Member exists');
       v_member_found:=true;
       exit;
    end if;
  end Loop SearchLoop;

  if not v_member_found then
    dbms_output.put_line('member does not exist'); 
  end if;

END;

Since SYS.Odcinumberlist is a VARRAY and comparison is not supported. 由于SYS.Odcinumberlist是VARRAY,因此不支持比较。 You can go with nested table type as describes below.. 您可以按照以下说明使用嵌套表类型。

SET SERVEROUTPUT ON;
DECLARE
TYPE v_list_tab
IS
  TABLE OF NUMBER;
  v_list v_list_tab:=v_list_tab(1,2,3,4,5);
BEGIN
  FOR i IN 1..v_list.COUNT
  LOOP
    dbms_output.put_line( v_list(i) );
  END LOOP;
  IF 1 MEMBER OF v_list THEN
    dbms_output.put_line( 'yes' );
  ELSE
    dbms_output.put_line( 'no' );
  END IF;
END; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM