Oracle:创建一个返回一组值的函数?

时间:2015-01-22 22:27:42

标签: oracle function

我有一个关键字表

select * from keywords;

id kw    
-- --    
 1 foo 
 1 bar   
 2 foo

以及将根据这些关键字从主表中选择行的查询。

select id, stuff from assets
 where id in (select unique id from keywords where kw = 'foo');

id stuff
-- -----
 1   ...
 2   ...

如何将子查询转换为函数?即我希望函数返回一组可由IN子句使用的值。

select id, stuff from assets
 where id in HAS_KEYWORD('foo');

1 个答案:

答案 0 :(得分:1)

表名有所不同,但解决方案有效

    create table tab (  
      key number,
      val number
      );

    create table foe(
      col1 number,
      col2 varchar2(3)
    );

    insert into tab values (0,3);
    insert into tab values (1,4);
    insert into tab values (2,5);

    insert into foe values (3,'YES');
    insert into foe values (4,'YES');
    insert into foe values (5,'NO');

    create type t_return is table of number;
    /
    create or replace function fnc(str varchar2) 
      return t_return pipelined is
    begin
      for rec in (select col1 from foe where col2 = str) loop
        pipe row(rec.col1);
      end loop;
    end;
    /

    select * from tab where val in (select * from table(fnc('YES')))