设为首页 加入收藏

TOP

Oracle中函数如何返回结果集
2015-07-24 12:01:14 来源: 作者: 【 】 浏览:21
Tags:Oracle 函数 如何 返回 结果

Oracle中,用函数返回结果集有时候要用到,下面是demo:

create or replace type t_test as object
(
id integer,
create_time date,
object_name varchar2(60)
);
create or replace type t_test_table as table of t_test;



1.用数组的方式

create or replace function f_test(n in number default null)
return t_test_table as
v_test t_test_table := t_test_table();
begin
for i in 1 .. n loop
v_test.extend();
v_test(v_test.count) := t_test(i, sysdate, 'name' || i);
end loop;
return v_test;
end f_test;
/

SQL> select * from table(f_test(5));
ID CREATE_TIME OBJECT_NAME
-------- -------------- -------------
1 07-4月 -15 name1
2 07-4月 -15 name2
3 07-4月 -15 name3
4 07-4月 -15 name4
5 07-4月 -15 name5



2.用管道函数
?

create or replace function f_test_pipe(n in number default null)
return t_test_table
PIPELINED as
v_test t_test_table := t_test_table();
begin
for i in 1 .. nvl(n, 100) loop
pipe row(t_test(i, sysdate, 'name' || i));
end loop;
return;
end f_test_pipe;
/


SQL> select * from table(f_test_pipe(5));
ID CREATE_TIME OBJECT_NAME
---------- -------------- ----------------
1 07-4月 -15 mc1
2 07-4月 -15 mc2
3 07-4月 -15 mc3
4 07-4月 -15 mc4
5 07-4月 -15 mc5

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracledb11.2.0.4linux6.3下编译b.. 下一篇Oracle,mysql,sqlserver,postgres..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: