12

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)
4

3 回答 3

19

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));
于 2010-01-13T21:49:53.927 回答
3

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

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

要使用这种机制,用户应该:

  1. 打开会话,
  2. 将所需的值分配给该包成员,
  3. SELECT视图中的数据,
  4. 做其他事情或关闭会话。

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

于 2012-02-02T12:02:52.190 回答
-2

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

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

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

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

于 2011-10-27T12:33:41.223 回答