How to conditionally join a table function in Oracle SQL, which has a primary table row column as an argument, without excessive function calls?

12yoGirl

I have the following issue: Every row in my base table have a flag column ('flg' in the listing below) and an function argument column ('arg'). If in a row #N the flag is 'Y', then function has to be called; after, it shall return, let's say, a column (actually a bunch of them, but i'll simplify as much as i can). And finally, the row #N should transform into a sub-table - full join of a row #N and the column, returned by the function. If the flag is 'N' then a result for the row #N shall be as the row itself plus NULL in that "function return column".

So here's an example:

create or replace package SIEBEL.TEST_PACKAGE as
    type ret_type is table of number;
    function testFunc(inp number)
    return ret_type
    pipelined;
end TEST_PACKAGE;
/
create or replace package body SIEBEL.TEST_PACKAGE is
    function testFunc(inp number)
    return ret_type
    pipelined
    is
    i number;
    begin
        dbms_output.put_line('Function call, arg = ' || to_char(inp));
        if (inp is null OR inp = 0) then
            pipe row (null);
        else
            for i in 1..inp loop
                 pipe row (i);
            end loop;
        end if;
    end testFunc;
end TEST_PACKAGE;
/
with base_table as
(
    select 'Shall invoke' col0, 'Y' flg, '2' arg from dual
    union
    select 'Shall not invoke' col0, 'N' flg, '0' arg from dual
)
select * from base_table t0, table(siebel.test_package.testFunc(t0.arg)) t1;

It will return what i actually want:

COL0              | FLG | ARG | COLUMN_VALUE
--------------------------------------------
Shall invoke      |  Y  |  2  |      1
Shall invoke      |  Y  |  2  |      2
Shall not invoke  |  N  |  0  |

The thing is, even for the 'N' flag the function is still called - the database output will show

Function call, arg = 2
Function call, arg = 0

If a real world i have hundreds of records with 'Y' flag, ~100K with 'N'. Alse my actual function is much more complicated and have a lot of stuff in its namespace, so every function call is crucial in terms of perfomance.

What i do want is the database output for the example:

Function call, arg = 2

I could achieve it with

with base_table as
    (
        select 'Shall invoke' col0, 'Y' flg, '2' arg from dual
        union
        select 'Shall not invoke' col0, 'N' flg, '0' arg from dual
    )
select t.*, t1.column_value
    from base_table t, table(SIEBEL.TEST_PACKAGE.testFunc(t.arg)) t1
    where t.flg = 'Y'
union all
select t.*, null as column_value
    from base_table t 
    where t.flg = 'N';

but then all indexes became useless - every 'order by' instruction will take alot to complete.

Please, help me to achieve the desired behaviour of function calls and still to save the primal rows order.

Feel free to ask me if anything is not clear.

Best Regards, Alexey

hol

For flag = "N" the function is not called if you conditionally join

set serveroutput on
with base_table as
(
    select 'Shall invoke' col0, 'Y' flg, '2' arg from dual
    union
    select 'Shall not invoke' col0, 'N' flg, '0' arg from dual
)
select * from base_table t0
left join table( test_package.testFunc(t0.arg) ) t1 on (t0.flg = 'Y');

Script Output

Package created.
Package body created.

COL0             FLG ARG COLUMN_VALUE
---------------- --- --- ------------
Shall invoke     Y   2              1
Shall invoke     Y   2              2
Shall not invoke N   0               

3 rows selected.

Server Output:

Function call, arg = 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a column of a join for argument the a function table in postgresql

From Dev

How to rename a column which contains count function in a joined table in sql?

From Dev

SQL Join a row table with a column table

From Dev

SQL - How to put a condition for which table is selected without left join

From Dev

Oracle SQL Developer - Pivot table without aggregation function

From Dev

How to get data in chunks from very large table which has primary key column data type as varchar

From Dev

How to get data in chunks from very large table which has primary key column data type as varchar

From Dev

Populating Row Number on new column for table without primary key

From Dev

Oracle PL/SQL: How to use a column conditionally as it may not exist in older versions of a table

From Dev

Oracle: Is it possible to choose which table to join based on a column value?

From Dev

table of structs in function argument

From Java

How can I conditionally style a Table Cell, Row, or Column in Slate?

From Dev

how to do a function to return row type from a table in pl/sql?

From Dev

How to select a table based on a value in a analytical function Oracle SQL

From Dev

SQL Inner join with function returning table

From Dev

php not "echoing" table row as a link that calls a javaScript function?

From Dev

Deal with blank row which returns from Comma Separated List To Table Function in SQL Server

From Dev

Reading a table when function has many argument return an error

From Dev

how to play with a function which return table in lua?

From Dev

table.row is not a function

From Dev

How to write a function in T-SQL, which accepts a table as input and returns result back as a table?

From Dev

How to pass a member function which has variable arguments as template argument?

From Dev

SQL Server : sorting a distinct table which has XML column

From Dev

How to apply a different multi-argument function to each row of a data.table?

From Dev

How can I insert data to a table which has a foreign key, from a table which has a primary key auto-increment?

From Dev

How to join the table based on main table column value in SQL Server?

From Dev

Pass an aliased column into a function for use in data.table `by=` argument

From Dev

Postgresql function which locks table, updates value and returns row

From Dev

How to make function NOT highlight last row in table?

Related Related

  1. 1

    Using a column of a join for argument the a function table in postgresql

  2. 2

    How to rename a column which contains count function in a joined table in sql?

  3. 3

    SQL Join a row table with a column table

  4. 4

    SQL - How to put a condition for which table is selected without left join

  5. 5

    Oracle SQL Developer - Pivot table without aggregation function

  6. 6

    How to get data in chunks from very large table which has primary key column data type as varchar

  7. 7

    How to get data in chunks from very large table which has primary key column data type as varchar

  8. 8

    Populating Row Number on new column for table without primary key

  9. 9

    Oracle PL/SQL: How to use a column conditionally as it may not exist in older versions of a table

  10. 10

    Oracle: Is it possible to choose which table to join based on a column value?

  11. 11

    table of structs in function argument

  12. 12

    How can I conditionally style a Table Cell, Row, or Column in Slate?

  13. 13

    how to do a function to return row type from a table in pl/sql?

  14. 14

    How to select a table based on a value in a analytical function Oracle SQL

  15. 15

    SQL Inner join with function returning table

  16. 16

    php not "echoing" table row as a link that calls a javaScript function?

  17. 17

    Deal with blank row which returns from Comma Separated List To Table Function in SQL Server

  18. 18

    Reading a table when function has many argument return an error

  19. 19

    how to play with a function which return table in lua?

  20. 20

    table.row is not a function

  21. 21

    How to write a function in T-SQL, which accepts a table as input and returns result back as a table?

  22. 22

    How to pass a member function which has variable arguments as template argument?

  23. 23

    SQL Server : sorting a distinct table which has XML column

  24. 24

    How to apply a different multi-argument function to each row of a data.table?

  25. 25

    How can I insert data to a table which has a foreign key, from a table which has a primary key auto-increment?

  26. 26

    How to join the table based on main table column value in SQL Server?

  27. 27

    Pass an aliased column into a function for use in data.table `by=` argument

  28. 28

    Postgresql function which locks table, updates value and returns row

  29. 29

    How to make function NOT highlight last row in table?

HotTag

Archive