简体   繁体   中英

Oracle SQL: how to pass parameter to a function that is used in a view through a column in a view?

Let's say we have such view that uses function with hard-coded parameter:

CREATE OR REPLACE VIEW x AS
SELECT t.some_value
  FROM table(function(p1 => '1')) t;

If I'd like to pass that parameter to a function through a view, what are possible options? Please mind that using global or context/bind variables is not an option. So far I've came up with an option to use a table that holds all available parameter values (keys) that could be passed to a view:

CREATE OR REPLACE VIEW x AS
SELECT st.input_param,
       t.some_value
  FROM some_table st
       table(function(p1 => st.input_param)) t;

However, I am wondering if there are any other possible options?

You can't pass a parameter to a view but you can use the next alternative:

CREATE TYPE RECORDS_VARCHAR AS TABLE OF VARCHAR2(100);

create or replace function virtual_table( input_param number )
return RECORDS_VARCHAR
PIPELINED
is

begin
FOR a IN (
            select '1' AS VALUE from dual where input_param = 2
            UNION ALL
            select '8' AS VALUE from dual
          ) loop
pipe row (a.VALUE);
end loop;
return;
end;


SELECT * FROM TABLE(virtual_table(2)); --1,8

SELECT * FROM TABLE(virtual_table(1)); --8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM