使用流水线函数从多表join获取结果集。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用流水线函数从多表join获取结果集。相关的知识,希望对你有一定的参考价值。
我想创建一个函数来获取表的选择性,输入参数是所有者和表名,我搜索了一下,发现这里可以使用pipelined函数,我使用了下面的代码,但我创建函数时出现了错误。
create or replace type ROW_TYPE as object
(
column_name varchar2(128),
num_rows number,
num_distinct number
);
/
create or replace type TABLE_TYPE as table of ROW_TYPE;
/
create or replace function getSelectivity(owner in varchar2, tab_name in varchar2)
return TABLE_TYPE
PIPELINED
IS
BEGIN
for myrow in(
select a.column_name,b.num_rows,a.num_distinct
from dba_tab_col_statistics a, dba_tables b
where a.owner = b.owner and a.table_name = b.table_name and a.owner=upper(owner) and
a.table_name =upper(tab_name)
)loop
pipe row(ROW_TYPE(myrow.column_name,myrow.num_rows,myrow.num_distinct));
end loop;
return;
end;
/
错误:
SQL> show errors;
Errors for FUNCTION MOVIL.GETSELECTIVITY:
LINE/COL ERROR
-------- -------------------------------
9/88 PL/SQL: ORA-00918: undefined column
7/14 PL/SQL: SQL Statement ignored
12/20 PLS-00364: Invalid usage of Loop index variable 'MYROW'
12/2 PL/SQL: Statement ignored
但如果我只查询一张表,那么一切都很好。
create or replace type ROW_TYPE as object
(
column_name varchar2(128),
num_distinct number
);
/
create or replace type TABLE_TYPE as table of ROW_TYPE;
/
create or replace function getSelectivity(owner in varchar2, tab_name in varchar2)
return TABLE_TYPE
PIPELINED
IS
BEGIN
for myrow in(
select a.column_name,a.num_distinct
from dba_tab_col_statistics a
where a.owner=upper(owner) and a.table_name =upper(tab_name)
)loop
pipe row(ROW_TYPE(myrow.column_name,myrow.num_distinct));
end loop;
return;
end;
/
pipelined函数不支持多表连接吗?
答案
你可以创建一个像下面这样的表的视图,并在你的函数中使用它。
create or replace view test_db_view as select
a.column_name,b.num_rows,a.num_distinct,
a.table_name,a.owner
from all_tab_col_statistics a, all_tables b
where a.owner = b.owner and a.table_name = b.table_name
create or replace function getSelectivity(owner in varchar2, tab_name in varchar2)
return TABLE_TYPE
PIPELINED
IS
BEGIN
for myrow in( select * from test_db_view a where a.owner=upper(owner) and
a.table_name =upper(tab_name)
)loop
pipe row(ROW_TYPE(myrow.column_name,myrow.num_rows,myrow.num_distinct));
end loop;
return;
end;
或者你可以尝试使用 p_owner
而不是 owner
按功能 getSelectivity
参数
以上是关于使用流水线函数从多表join获取结果集。的主要内容,如果未能解决你的问题,请参考以下文章