码迷,mamicode.com
首页 > 数据库 > 详细

Oracle Table Function

时间:2014-05-15 03:15:18      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:table function   sql   oracle   

Oracle Table Function在Oracle9i时引入,完美的兼容了view和存储过程的优点:

bubuko.com,布布扣

应用举例:

1.Table()函数:

set feedback off

create or replace type type_int_date_varchar2  as object (i integer, d date, v varchar2(99))
/

create or replace type table_int_date_varchar2 as table of 
type_int_date_varchar2
/

create or replace function f_int_date_varchar2 return table_int_date_varchar2 as
  a_type_int_date_varchar2 table_int_date_varchar2 := table_int_date_varchar2();
begin

  a_type_int_date_varchar2.extend;
  a_type_int_date_varchar2(a_type_int_date_varchar2.count) := 
    type_int_date_varchar2(1,to_date(‘08.01.1947‘,‘dd.mm.yyyy‘), ‘David Bowie‘);

  a_type_int_date_varchar2.extend;
  a_type_int_date_varchar2(a_type_int_date_varchar2.count) := 
    type_int_date_varchar2(2,to_date(‘27.01.1756‘,‘dd.mm.yyyy‘), ‘Mozart‘);

  return a_type_int_date_varchar2;
end;
/

select * from table (cast (f_int_date_varchar2() as table_int_date_varchar2));

drop function f_int_date_varchar2;
drop type table_int_date_varchar2;
drop type type_int_date_varchar2;

2.A Function that returns a table of dates in a Range

create or replace type date_obj as object (dt date)
/

create or replace type date_table as table of date_obj
/


create or replace function date_range(from_dt in date, to_dt in date) 
  return date_table as
    a_date_table date_table := date_table();
    cur_dt date:=from_dt;
  begin
    while cur_dt <= to_dt loop
      a_date_table.extend;
      a_date_table(a_date_table.count) := date_obj(cur_dt);
      cur_dt := cur_dt + 1;
    end loop;
  return a_date_table;
end date_range;
/
select * from  table (
cast ( date_range(
    to_date(‘01.01.2002‘,‘dd.mm.yyyy‘),
    to_date(‘31.01.2002‘,‘dd.mm.yyyy‘)
  ) 
as date_table
));


Oracle Table Function,布布扣,bubuko.com

Oracle Table Function

标签:table function   sql   oracle   

原文地址:http://blog.csdn.net/davidwang9527/article/details/25697611

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!