Oracle杂文:table函数使用

来源:互联网 发布:传输文件到阿里云 编辑:程序博客网 时间:2024/05/17 23:45
  • 前言

使用table函数,可在不建表的情况下,可与其他处理逻辑的fuction相结合,达到完成运算后再展现数据集的效果。
现阶段,本人主要将此用在接口、报表两个方面。

  • 创建类型t_test

create or replace type t_test as object (
id number,
time date,
data varchar2(60)
);

  • 创建以t_test类型的数组t_test_tb

create or replace type t_test_tb as table of t_test;

  • 创建普通函数 返回数组类型,用数组的实例存放结果集(t_array),结果集全部处理完成后一起返回

create or replace function f_test_array(n in number default null) return t_test_tb as
t_array t_test_tb := t_test_tb();
begin
for i in 1..nvl(n,100) loop
t_array.extend();
t_array(t_array.count) := t_test(i,sysdate,'mi'||i);
end loop;
return t_array;
end;

  • 调用示例

select * from table(f_test_array(10)); select * from the(select f_test_array(10) from dual);