oracle-databasepipelined-function

Oracle: Translate subselect into pipelined function?


How do I generically translate a subselect into a pipelined function call?

For example how would I translate this:

select id, stuff from t1 where id in (select unique id from kw where k = 'foo')

to this:

select id, stuff from t1 where id in (select id from table(has_kw('foo'))

Solution

  • Writing the pipelined table function is relatively straightforward

    CREATE TYPE num_tbl AS TABLE OF NUMBER;
    
    CREATE FUNCTION has_kw( p_k IN VARCHAR2 )
      RETURN num_tbl
      PIPELINED
    IS
    BEGIN
      FOR i IN (SELECT DISTINCT id FROM kw WHERE k = p_k)
      LOOP
        PIPE ROW( i.id );
      END LOOP;
      RETURN;
    END;
    

    Now, I'm not sure that it would really make a whole lot of sense to use a pipeliend table function here. But perhaps your actual use case is more complicated and a pipelined table function would be more appropriate.