【问题标题】:Getting "local collection types not allowed in SQL statements" even though I use "table" operator即使我使用“表”运算符,也得到“SQL 语句中不允许的本地集合类型”
【发布时间】:2020-03-07 11:13:28
【问题描述】:

我有一个过程,我试图在 where 语句中使用一个集合。

 procedure purge_table is

    type t_ids is table of number;
    arr_ids    t_ids ;

   begin  
   select distinct (s.id) bulk collect
              into arr_ids    
        from students s;

   select count(*)
      into v_deleted_row_count
      from students s
      where s.id in (select * from table(arr_ids));

   end;

对于包含 where 语句的行,我收到“本地集合类型不允许 i SQL 语句”。 据我所知,我搜索了错误,但我的语法是正确的,但我不明白是什么 “假设你的集合是在 SQL 中定义的,而不仅仅是在 PL/SQL 中,你可以使用 TABLE 运算符” 在此处接受的答案中提到:Array in IN() clause oracle PLSQL

这里也是公认的答案

https://dba.stackexchange.com/questions/141325/how-can-i-use-an-array-variable-inside-the-in-operator-for-oracle-sql

建议和我做的一样。

我猜它必须与在 SQL 中定义一个集合有关。你能帮我解决这个问题吗?

【问题讨论】:

    标签: oracle stored-procedures plsql plsqldeveloper


    【解决方案1】:

    类型应该在 SQL 级别创建,然后在您的 PL/SQL 过程中使用。像这样的东西(基于 Scott 的模式):

    SQL> set serveroutput on
    SQL> create or replace type emp_tab as table of number;
      2  /
    
    Type created.
    
    SQL> create or replace procedure purge_table is
      2      arr_ids    emp_tab := emp_tab();
      3      v_deleted_row_Count number;
      4  begin
      5     select distinct (s.empno) bulk collect
      6       into arr_ids
      7       from emp s where deptno = 10;
      8
      9     select count(*)
     10        into v_deleted_row_count
     11        from emp s where s.empno in (select * from table(arr_ids));
     12
     13     dbms_output.put_line('Number = ' || v_deleted_row_count);
     14  end;
     15  /
    
    Procedure created.
    
    SQL> exec purge_table;
    Number = 3
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多