2012-01-13 61 views
3

我想將一個字符串列表傳遞給一個過程,並在select語句的WHERE子句中使用它,但我無法弄清楚這個怎麼做。將一個字符串數組傳遞給一個過程,並在WHERE子句中用IN

我已經宣佈了以下嵌套表:

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; 

如何才能做到這一點?

回答

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 
+0

+1這很煩人,我們還是要做到這一點,但很可惜我們這樣做。 – APC 2012-01-13 10:05:26

相關問題