par*_*par 8 oracle collections
我在oracle代码块中使用集合,因为没有表变量(如在MS SQL Server中).
DECLARE
TYPE I_NAME IS TABLE OF NVARCHAR2(512);
I_ITEMNAME I_NAME := I_NAME();
BEGIN
Run Code Online (Sandbox Code Playgroud)
我正在使用"BULK COLLECT INTO I_ITEMNAME"来填充集合.
我想在SELECT查询的WHERE子句中使用此集合,但无法找到执行此操作的方法.目前我正在使用FOR循环并逐个获取项目.
我怎样才能在WHERE子句中直接使用集合
SELECT*FROM TBL WHERE COL IN I_ITEMNAME?
谢谢,
Ale*_*ole 13
您不能在SQL子句中使用本地声明的集合:
declare
type i_name is table of nvarchar2(512);
i_itemname i_name := i_name();
c number;
begin
select distinct owner bulk collect into i_itemname from all_objects;
dbms_output.put_line(i_itemname.count);
select count(*) into c
from all_tables
where owner in (select * from table(i_itemname));
dbms_output.put_line(c);
end;
/
where owner in (select * from table(i_itemname));
*
ERROR at line 10:
ORA-06550: line 10, column 41:
PLS-00642: local collection types not allowed in SQL statements
ORA-06550: line 10, column 35:
PL/SQL: ORA-22905: cannot access rows from a non-nested table item
ORA-06550: line 8, column 5:
PL/SQL: SQL Statement ignored
Run Code Online (Sandbox Code Playgroud)
但是如果它在模式级别声明,那么你可以基本上让SQL知道类型,而不仅仅是PL/SQL:
create type i_name is table of nvarchar2(512);
/
Type created.
declare
i_itemname i_name := i_name();
c number;
begin
select distinct owner bulk collect into i_itemname from all_objects;
dbms_output.put_line(i_itemname.count);
select count(*) into c from all_tables
where owner in (select * from table(i_itemname));
dbms_output.put_line(c);
end;
/
No errors.
18
128
PL/SQL procedure successfully completed.
Run Code Online (Sandbox Code Playgroud)
您也可以加入table构造而不是使用子查询:
...
select count(*) into c
from table(i_itemname) t
join all_tables at on at.owner = t.column_value;
...
Run Code Online (Sandbox Code Playgroud)
我不太清楚你在做什么.(如果你没有将这个集合用于其他任何事情,你最好只加入原始数据,但我认为这个集合是有原因的).
正如@haki在评论中提到的,你也可以这样做:
...
select count(*) into c
from all_tables
where owner member of (i_itemname);
...
Run Code Online (Sandbox Code Playgroud)
... ...只要i_name和您比较的列是相同的类型.在我的例子找到零行,因为我想比较nvarchar2有varchar2,但如果重新定义会找到一个匹配i_name的varchar2(512).无论如何,在你的情况下可能tab.col是这样nvarchar2.
| 归档时间: |
|
| 查看次数: |
12345 次 |
| 最近记录: |