我有一个过程,我试图在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语句的行,我得到了“ SQL语句中不允许使用本地集合类型”。 据我所知语法正确,我一直在搜索错误,但我不知道该怎么做 “假设您的集合是在SQL中定义的,而不仅仅是在PL / SQL中定义,则可以使用TABLE运算符” 此处在接受的答案中提到:Array in IN() clause oracle PLSQL。
也是这里接受的答案
建议与我做同样的事情。
我想它必须与在SQL中定义一个集合有关。你能帮我吗?
答案 0 :(得分:2)
类型应在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>