问题描述:
在开发中,我们会遇到下面的情形,在PL/SQL程序中定义了type类型的集合,当引用的时候出现PLS-00642错误:
PLS-00642: local collection types not allowed in SQL statements
举例:
SQL> DECLARE
2 TYPE id IS TABLE OF NUMBER;
3 v_id id := id(649605799, 649605800, 649605801, 649605802);
4 BEGIN
5 FOR rec IN (SELECT column_value id FROM TABLE(v_id))
6 LOOP
7 dbms_output.put_line('ID:' || rec.id);
8 END LOOP;
9
10 END;
11 /
FOR rec IN (SELECT column_value id FROM TABLE(v_id))
*
ERROR at line 5:
ORA-06550: line 5, column 49:
PLS-00642: local collection types not allowed in SQL statements
ORA-06550: line 5, column 43:
出现这种问题的原因是,SQL语句中使用了本地定义的Type类型,Type必须定义为Schema级别的才可以使用。
解决办法:
在Schema级别定义Type,即使用CREATE TYPE语法定义,而不是在PL/SQL子程序中定义。
SQL> CREATE OR REPLACE TYPE id AS TABLE OF NUMBER;
2 /
Type created.
SQL> DECLARE
2 v_id id := id(649605799, 649605800, 649605801, 649605802);
3 BEGIN
4 FOR rec IN (SELECT column_value id FROM TABLE(v_id))
5 LOOP
6 dbms_output.put_line('ID:' || rec.id);
7 END LOOP;
8
9 END;
10 /
PL/SQL procedure successfully completed.
SQL>