简体   繁体   English

Oracle SQL:如何通过视图中的列将参数传递给视图中使用的 function?

[英]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:假设我们有这样的视图,它使用带有硬编码参数的 function:

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?如果我想通过视图将该参数传递给 function,有哪些可能的选项? 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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