从employee表中选择index不在集合中

时间:2013-01-09 05:52:20

标签: oracle plsql

我有一个如下程序:

SET serveroutput ON 
    DECLARE 
        TYPE list_of_names_t 
          IS TABLE OF emp.emp_index%TYPE; 
        ignoreIndexes LIST_OF_NAMES_T := List_of_names_t();
    BEGIN
    -- Logic here which fills the values in the collection ignoreIndexes and           considerIndexes  
    -- Line XX   
END;

在第XX行,我想添加以下行

SELECT * FROM emp WHERE emp_index NOT IN ignoreIndexes但是无法获得正确的语法,如何做到这一点?

1 个答案:

答案 0 :(得分:0)

如果集合类型是在schema level声明的,则可以在SQL statements中动态使用,也可以动态使用。请执行以下步骤以使其正常工作。

  1. 创建或替换类型list_of_names AS OBJECT(emp_index NUMBER);

  2. 创建或替换类型list_of_names_t是list_of_names;

  3. 创建您的匿名块,如下所示:

    SET serveroutput ON 
    DECLARE 
       ignoreIndexes LIST_OF_NAMES_T := List_of_names_t();
       emp_cur  SYS_REFCURSOR;
    BEGIN
       -- Populate the collection with some data
     SELECT list_of_names(LEVEL) 
         BULK COLLECT INTO ignoreIndexes  
     FROM dual CONNECT BY LEVEL<21;
    
    
    OPEN emp_cur FOR 
              SELECT * 
               FROM EMP e 
                   WHERE NOT EXISTS(SELECT 1 
                                     FROM   table(ignoreIndexes) tt 
                                     WHERE  tt.emp_index = e.emp_index
                                    );   
    
    END;