Oracle 中函数如何返回结果集

WBOY
풀어 주다: 2016-06-07 16:08:58
원래의
1428명이 탐색했습니다.

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

在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

本文永久更新链接地址

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!