SQL 表达式类型错误
SQL expression is of wrong type
正在 运行 SQL 开发人员
进行以下操作
declare
type hType is table of number not null
index by BINARY_INTEGER;
items_v hType;
references_v hType;
keyId number;
begin
keyId := 1234567;
select i.item bulk collect into items_v
from items i
where i.item_id = ...;
select j.reference bulk collect into references_v
from references j
where j.reference = ..
and j.item in items_v;
end;
I get ORA-06550: line 23, column 31: PLS-00382: expression is of the
wrong type
即使 j.item
和 i.item
也是类型 NUMBER
;
在查询的 REFERENCES 部分,您不能那样使用 ITEMS_V 集合;考虑改用这样的东西:
for i in items_v.first .. items_v.last loop
select j.reference bulk collect into references_v
from references j
where j.reference = ..
and j.item_id = items_v(i);
end loop;
可能有更聪明的方法来做到这一点(希望其他人会告诉您如何做),但这也应该有效。
直到 11g,您不能在 PLSQL
范围内声明 TYPE
并在 PLSQL 块内的 SQL
语句中使用它。如果要在 PLSQL
块下使用它,则需要在 SQL
范围内声明 TYPE
。此外,如果您想在 IN
子句中使用一个集合,则可以使用 MEMBER OF
。见下文:
类型创建于 SQL 范围。
SQL> CREATE OR REPLACE TYPE HTYPE IS TABLE OF NUMBER NOT NULL ;
/
Type created.
匿名块
SQL> DECLARE
2 ITEMS_V HTYPE;
3 REFERENCES_V HTYPE;
4 KEYID NUMBER;
5 BEGIN
6 KEYID := 1234567;
7
8 SELECT I.ITEM
9 BULK COLLECT INTO ITEMS_V
10 FROM ITEMS I
11 WHERE I.ITEM_ID = 1;
12
13 SELECT J.REFERENCE
14 BULK COLLECT INTO REFERENCES_V
15 FROM REFERENCES J
16 WHERE J.REFERENCE = 1
17 AND J.ITEM MEMBER OF ITEMS_V;--<--Used Member of to check elements of colection
18 END;
19 /
PL/SQL procedure successfully completed.
您可以使用 IN
子句作为:
SQL> DECLARE
2 ITEMS_V HTYPE;
3 REFERENCES_V HTYPE;
4 KEYID NUMBER;
5 BEGIN
6 KEYID := 1234567;
7
8 SELECT I.ITEM
9 BULK COLLECT INTO ITEMS_V
10 FROM ITEMS I
11 WHERE I.ITEM_ID = 1;
12
13 SELECT J.REFERENCE
14 BULK COLLECT INTO REFERENCES_V
15 FROM REFERENCES J
16 WHERE J.REFERENCE = 1
17 AND J.ITEM IN (SELECT COLUMN_VALUE FROM TABLE(ITEMS_V)); --<-- Used IN Clause.
18 END;
19 /
PL/SQL procedure successfully completed.
正在 运行 SQL 开发人员
进行以下操作declare
type hType is table of number not null
index by BINARY_INTEGER;
items_v hType;
references_v hType;
keyId number;
begin
keyId := 1234567;
select i.item bulk collect into items_v
from items i
where i.item_id = ...;
select j.reference bulk collect into references_v
from references j
where j.reference = ..
and j.item in items_v;
end;
I get ORA-06550: line 23, column 31: PLS-00382: expression is of the wrong type
即使 j.item
和 i.item
也是类型 NUMBER
;
在查询的 REFERENCES 部分,您不能那样使用 ITEMS_V 集合;考虑改用这样的东西:
for i in items_v.first .. items_v.last loop
select j.reference bulk collect into references_v
from references j
where j.reference = ..
and j.item_id = items_v(i);
end loop;
可能有更聪明的方法来做到这一点(希望其他人会告诉您如何做),但这也应该有效。
直到 11g,您不能在 PLSQL
范围内声明 TYPE
并在 PLSQL 块内的 SQL
语句中使用它。如果要在 PLSQL
块下使用它,则需要在 SQL
范围内声明 TYPE
。此外,如果您想在 IN
子句中使用一个集合,则可以使用 MEMBER OF
。见下文:
类型创建于 SQL 范围。
SQL> CREATE OR REPLACE TYPE HTYPE IS TABLE OF NUMBER NOT NULL ;
/
Type created.
匿名块
SQL> DECLARE
2 ITEMS_V HTYPE;
3 REFERENCES_V HTYPE;
4 KEYID NUMBER;
5 BEGIN
6 KEYID := 1234567;
7
8 SELECT I.ITEM
9 BULK COLLECT INTO ITEMS_V
10 FROM ITEMS I
11 WHERE I.ITEM_ID = 1;
12
13 SELECT J.REFERENCE
14 BULK COLLECT INTO REFERENCES_V
15 FROM REFERENCES J
16 WHERE J.REFERENCE = 1
17 AND J.ITEM MEMBER OF ITEMS_V;--<--Used Member of to check elements of colection
18 END;
19 /
PL/SQL procedure successfully completed.
您可以使用 IN
子句作为:
SQL> DECLARE
2 ITEMS_V HTYPE;
3 REFERENCES_V HTYPE;
4 KEYID NUMBER;
5 BEGIN
6 KEYID := 1234567;
7
8 SELECT I.ITEM
9 BULK COLLECT INTO ITEMS_V
10 FROM ITEMS I
11 WHERE I.ITEM_ID = 1;
12
13 SELECT J.REFERENCE
14 BULK COLLECT INTO REFERENCES_V
15 FROM REFERENCES J
16 WHERE J.REFERENCE = 1
17 AND J.ITEM IN (SELECT COLUMN_VALUE FROM TABLE(ITEMS_V)); --<-- Used IN Clause.
18 END;
19 /
PL/SQL procedure successfully completed.