如何检查数组是否包含特定字符串?

时间:2013-01-29 08:12:51

标签: oracle plsql contains user-defined-types

我有一个字符串数组。我想检查数组中是否存在特定的字符串。

DECLARE
  TYPE v_array IS TABLE OF VARCHAR2(200);
  ais_array v_array;
BEGIN
  ais_array := ('Lb1','Lb2','Lb3','Lb613');
  IF 'Lb1' IN ais_array THEN
     dbms_output.put_line('found');
  END IF;
END;

IN运算符无效。我尝试对该类型执行select *,然后使用IN,但这也无效。

有什么建议吗?

2 个答案:

答案 0 :(得分:25)

尝试member of条件:

IF 'Lb1' member of ais_array THEN
  dbms_output.put_line('found');
END IF;

Oracle引入了几个集合运算符,用于处理10g中的集合。请阅读the documentation to learn more

答案 1 :(得分:0)

MEMBER OF只能与嵌套表一起使用。您正在尝试在关联数组上使用它。您将收到一个错误消息“调用'MEMBER OF'的参数数目或类型错误”。

这是使用方法:

    DECLARE  
   TYPE clientele IS TABLE OF VARCHAR2 (64);  

   client_list_12    clientele := clientele ('Customer 1', 'Customer 2');  
   client_list_13    clientele := clientele ('Customer 1', 'Customer 3');  

   client_list_133   clientele  
                   := clientele ('Customer 1', 'Customer 3', 'Customer 3');  

   client_list_empty clientele := clientele ();                          
BEGIN  
   IF 'Customer 1' MEMBER OF client_list_12  
   THEN  
      DBMS_OUTPUT.put_line ('Customer 1 is in the 12 list');  
   END IF;  

   IF 'Customer 2' NOT MEMBER OF client_list_13  
   THEN  
      DBMS_OUTPUT.put_line ('Customer 2 is not in the 13 list');  
   END IF;  

   DBMS_OUTPUT.put_line ('List 133 contains ' || CARDINALITY (client_list_133) || ' items');  

   IF client_list_empty IS EMPTY  
   THEN  
      DBMS_OUTPUT.put_line ('Client list is empty');  
   END IF;  

   IF client_list_133 IS NOT EMPTY  
   THEN  
      DBMS_OUTPUT.put_line ('Client list 133 is not empty');  
   END IF;  

END;