ORACLE 11g 中的表值函数? (参数化视图)

发布于 2024-08-17 13:54:23 字数 571 浏览 6 评论 0原文

我过去见过有关此问题的讨论,例如 这里。但我想知道是否在某个地方,也许是 10g 或 11g(我们正在使用 11g),ORACLE 引入了对“参数化视图”的更好支持,而不需要在数据库中乱扔各种用户定义的类型和/或游标定义或 sys_context 变量。

我希望 ORACLE 添加了对简单“正常工作”的支持,如 T-SQL 中的以下示例:

CREATE FUNCTION [dbo].[getSomeData] (@PRODID ROWID)  
RETURNS TABLE AS  
    RETURN SELECT PRODID, A, B, C, D, E  
    FROM MY_TABLE  
    WHERE PRODID = @PRODID

然后按如下方式选择它:

SELECT * FROM dbo.getSomeData(23)

I've seen discussions about this in the past, such as here. But I'm wondering if somewhere along the line, maybe 10g or 11g (we are using 11g), ORACLE has introduced any better support for "parameterized views", without needing to litter the database with all sorts of user-defined types and/or cursor definitions or sys_context variables all over.

I'm hoping maybe ORACLE's added support for something that simply "just works", as per the following example in T-SQL:

CREATE FUNCTION [dbo].[getSomeData] (@PRODID ROWID)  
RETURNS TABLE AS  
    RETURN SELECT PRODID, A, B, C, D, E  
    FROM MY_TABLE  
    WHERE PRODID = @PRODID

Then just selecting it as so:

SELECT * FROM dbo.getSomeData(23)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甲如呢乙后呢 2024-08-24 13:54:23

不需要 SYS_CONTEXT 或游标定义。
您确实需要一个类型,以便在解析 SQL 时确定要返回哪些列。
也就是说,您可以轻松编写一个脚本,根据 user_tab_columns 中的数据为一个或多个表生成类型和集合类型定义。

最接近的是

create table my_table
(prodid number, a varchar2(1), b varchar2(1), 
  c varchar2(1), d varchar2(1), e varchar2(1));

create type my_tab_type is object
(prodid number, a varchar2(1), b varchar2(1), 
  c varchar2(1), d varchar2(1), e varchar2(1))
.
/

create type my_tab_type_coll is table of my_tab_type;
/

create or replace function get_some_data (p_val in number) 
return my_tab_type_coll pipelined is
begin
  FOR i in (select * from my_table where prodid=p_val) loop
    pipe row(my_tab_type(i.prodid,i.a,i.b,i.c,i.d,i.e));
  end loop;
  return;
end;
/

SELECT * FROM table(get_Some_Data(3));

No need for SYS_CONTEXT or cursor definitions.
You do need a type so that, when the SQL is parsed, it can determine which columns are going to be returned.
That said, you can easily write a script that will generate type and collection type definitions for one or more tables based on the data in user_tab_columns.

The closest is

create table my_table
(prodid number, a varchar2(1), b varchar2(1), 
  c varchar2(1), d varchar2(1), e varchar2(1));

create type my_tab_type is object
(prodid number, a varchar2(1), b varchar2(1), 
  c varchar2(1), d varchar2(1), e varchar2(1))
.
/

create type my_tab_type_coll is table of my_tab_type;
/

create or replace function get_some_data (p_val in number) 
return my_tab_type_coll pipelined is
begin
  FOR i in (select * from my_table where prodid=p_val) loop
    pipe row(my_tab_type(i.prodid,i.a,i.b,i.c,i.d,i.e));
  end loop;
  return;
end;
/

SELECT * FROM table(get_Some_Data(3));
孤者何惧 2024-08-24 13:54:23

可以在 Oracle 中定义一种“参数化”视图。
步骤是:

  1. 定义一个包含实际上是所需参数的公共成员的包(该包中不需要函数或过程),
  2. 定义基于该包成员的视图。

要使用此机制,用户应该:

  1. 打开会话、
  2. 为包成员分配所需的值、
  3. 从视图中选择数据、
  4. 执行其他操作或关闭会话。

备注:用户必须在一个会话中完成所有三个步骤,因为包成员范围恰好是一个会话。

It is possible to define a kind of "parametrized" views in Oracle.
The steps are:

  1. Define a package containing as public members that are in fact the needed parameters (there is no need for functions or procedures in that package),
  2. Define a view that is based on that package members.

To use this mechanism one user should:

  1. open a session,
  2. assign the desired values to that package members,
  3. SELECT data from the view,
  4. do other stuff or close the session.

REMARK: it is essential for the user to do all the three steps in only one session as the package members scope is exactly a session.

云醉月微眠 2024-08-24 13:54:23

SQL SERVER 中有两种类型的表值函数:

  1. 内联表值函数:对于内联表值函数,没有函数体;该表是单个 SELECT 语句的结果集。该类型可以命名为
    据我所知,ORACLE 中没有对应的“参数化视图”。

  2. 多语句表值函数:对于多语句表值函数,在 BEGIN...END 块中定义的函数体包含一系列构建和插入的 Transact-SQL 语句行写入将返回的表中。

上面的示例(作者:Gary Myers)创建了第二种类型的表函数,它不是“参数化视图”。

There are TWO types of table-valued functions in SQL SERVER:

  1. Inline table-valued function: For an inline table-valued function, there is no function body; the table is the result set of a single SELECT statement. This type can be named as
    'parameterized view' and it has no equivalent in ORACLE as I know.

  2. Multistatement table-valued function: For a multistatement table-valued function, the function body, defined in a BEGIN...END block, contains a series of Transact-SQL statements that build and insert rows into the table that will be returned.

The above sample (By Gary Myers) creates a table function of the second type and it is NOT a 'parameterized view'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文