将一个字符串数组传递给一个过程,并在WHERE子句中使用它与IN一起使用

时间:2012-01-13 09:13:18

标签: oracle plsql

我想将一个字符串列表传递给一个过程并在select语句的WHERE子句中使用它,但我无法弄清楚如何执行此操作。

我已经声明了以下嵌套表:

TYPE t_strarray IS TABLE OF VARCHAR2(30);

程序如下:

PROCEDURE getstuff(p_list IN t_strarray, io_cursor OUT t_cursor)
   IS    
   BEGIN
        OPEN io_cursor FOR
            SELECT * FROM mytable 
            WHERE mytable.field1 in (select * from table(p_list)); 
   END;

如何做到这一点?

1 个答案:

答案 0 :(得分:8)

您的数组需要是SQL对象类型,直接在SQL中创建,而不是在包中声明的PLSQL类型:

SQL> CREATE OR REPLACE TYPE t_strarray IS TABLE OF VARCHAR2(30);
  2  /
Type created.

SQL> CREATE TABLE mytable (field1 VARCHAR2(30));
Table created.

SQL> INSERT INTO mytable VALUES ('A');
1 row created.

SQL> INSERT INTO mytable VALUES ('D');
1 row created.

SQL> CREATE OR REPLACE PROCEDURE getstuff(p_list IN t_strarray,
  2                                       io_cursor OUT SYS_REFCURSOR) IS
  3  BEGIN
  4     OPEN io_cursor FOR
  5        SELECT *
  6          FROM mytable
  7         WHERE mytable.field1 IN (SELECT COLUMN_VALUE FROM TABLE(p_list));
  8  END;
  9  /
Procedure created.

SQL> VARIABLE cc REFCURSOR;
SQL> EXEC getstuff (t_strarray('A', 'B', 'C'), :cc);    
PL/SQL procedure successfully completed.

SQL> print cc

FIELD1
------------------------------
A