我正在尝试创建一个select语句来选择在前一个select语句中选择的字段,如下面的代码所示(使用oracle 11g):
Declare
type NumberArray IS TABLE OF Number;
v_medium NumberArray;
v_medium_final NumberArray := NumberArray();
v_count number;
type VarcharArray IS TABLE OF VARCHAR2(60);
v_Type VarcharArray
--Select all Regions and Locality affected by the fault
cursor Locality_cur is
Select LocalityCode from AvaFaultLocality where FAULTID=36841 and SITEID=1;
Locality_rec Locality_cur%ROWTYPE;
cursor Region_cur is
Select RegionCode from avafaultregion where FAULTID=36841 and SITEID=1;
Region_rec Region_cur%ROWTYPE;
Begin
v_count := 1;
FOR Region_rec IN Region_cur
LOOP
dbms_output.put_line(Region_rec.RegionCode);
Select RECIPIENTMEDIUMID BULK COLLECT into v_medium from AvaRecipientAreaRegion where RegionCode = Region_rec.RegionCode;
FOR idx IN 1..v_medium.COUNT LOOP
v_medium_final.extend();
v_medium_final(v_count) := v_medium(idx);
dbms_output.put_line(TO_CHAR(v_medium(idx)));
v_count:= v_count + 1;
END LOOP;
dbms_output.put_line('End Loop for Region '||Region_rec.RegionCode);
END LOOP;
dbms_output.put_line(TO_CHAR(v_medium_final.count));
Select MEDIUMTYPECODE BULK COLLECT into v_Type from AvaRecipientMedium where RECIPIENTMEDIUMID in (SELECT * FROM TABLE(v_medium_final)); <--- Line with problem
End;
我要做的主要是选择所有区域的内容,循环此结果并获取这些区域中所有位置的内容,然后获取每个区域的所有RecipientMediumID并将结果添加到名为的变量中v_medium_final。这部分工作得很好。 问题是当我尝试使用此选择的结果来选择RecipientMedium表中的值时,其中所有值都在变量v_medium_final中。
我尝试使用:
Select MEDIUMTYPECODE BULK COLLECT into v_Type from AvaRecipientMedium where RECIPIENTMEDIUMID in (SELECT * FROM TABLE(v_medium_final));
或
Select MEDIUMTYPECODE BULK COLLECT into v_Type from AvaRecipientMedium where RECIPIENTMEDIUMID in v_medium_final;
但我总是得到错误:
[Error] Execution (32: 126): ORA-06550: line 32, column 126:
PLS-00642: local collection types not allowed in SQL statements
ORA-06550: line 32, column 120:
PL/SQL: ORA-22905: cannot access rows from a non-nested table item
ORA-06550: line 32, column 7:
PL/SQL: SQL Statement ignored
并针对第二种情况: [错误]执行(32:106):ORA-06550:第32行,第106列:
PLS-00642: local collection types not allowed in SQL statements
答案 0 :(得分:1)
如错误所示,您无法使用本地收集类型。您需要使用在SQL级别定义的集合类型。
CREATE TYPE NumberArray
AS TABLE OF NUMBER;
DECLARE
v_medium NumberArray;
...
这将要求您拥有CREATE TYPE
权限,这意味着任何有权限查看它的人都可以看到您的类型定义,而不是只有PL / SQL块的范围。
就个人而言,我会避免在Array
的集合类型名称中使用VARRAY
,因为名称可能会令人困惑。如果我看到类型NumberArray
,我猜它被定义为VARRAY(n) OF NUMBER
而不是嵌套表类型。对嵌套表类型使用ntt
后缀似乎更合理(create type number_ntt as table of number
)。