Oracle: Create a function that returns a set of values?

Mark Harrison

I have a table of keywords

select * from keywords;

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

and a query that will select rows from a master table based on those keywords.

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

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

How can I turn the subquery into a function? i.e. I would like a function to return a set of values that can be used by an IN clause.

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

Table names differ a bit but the solution works

    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')))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type hint for a function that returns only a specific set of values

Create foreach() .combine function that only returns unique values

The function returns invalid values

create a function that returns an object

How can I create a vector with values that are obtained by a function that returns different values for every row?

Create dynamic function in oracle

Oracle - Create a function that has the system date as a default parameter that returns all rows meeting a certain condition on the parameter

Oracle: How to create a function returning values for a "SELECT * FROM tab WHERE name IN (function())"

Create a function that accepts a function and returns a function with timestamps

Powershell function returns intermediate values

Print function returns two values

checkPrime function returns incorrect values

counter function returns wrong values

Function which returns multiple values

How do I unit test a function that returns multiples values from a set of mappings?

Trying to create a function that takes two long values and returns true if and only if the first value is a multiple of the second value

JavaScript: Create function that returns an object whose keys match the elements in the array of values using callbacks

How can I use numpy to create a function that returns two-dimensional values from arrays of data?

How to create function that returns nothing

Create function returns double value

MySQL Create function returns error

Create generic set function

Oracle returns wrong values with LENGTH and INSTR

passing correlated values to a function in Oracle

Decode function in oracle for null values

Oracle create function using cursors

JavaScript: Create Function that returns a function that returns output with getter / setter capabilities

Confused about how set in python returns values

minus a set of some constant values from a set of values of a column in oracle