Использование pipelined функции

Имеется таблица dept со следующей структурой:

Name   Type         Nullable Default Comments 
------ ------------ -------- ------- -------- 
DEPTNO NUMBER                                 
DNAME  VARCHAR2(14) Y                         
LOC    VARCHAR2(13) Y

Необходимо реализовать функцию PL/SQL которая будет возвращать выборку из таблицы dept заданную минимальным и максимальным значением поля DEPTNO. Реализуемая функция должна использовать метод pipelined.

reate or replace type debt_object as object
(
  DEPTNO NUMBER,
  DNAME  VARCHAR2(14),
  LOC    VARCHAR2(13)
)
;
/
create or replace type debt_table as table of debt_object;
/

Вариант №1

create or replace function function_pipelined return debt_table
  pipelined as
begin

  <<debt>>
  for i in (select d.*
              from (select d.* from debt d order by d.deptno asc) d
             where rownum = 1
            union all
            select d.*
              from (select d.* from debt d order by d.deptno desc) d
             where rownum = 1) loop
  
    pipe row(debt_object(i.DEPTNO, i.dname, i.loc));
  
  end loop debt;

  return;

end function_pipelined;
/

Вариант №2

create or replace function function_pipelined return debt_table
  pipelined as
begin

  <<debt>>
  for i in (select d.*
              from debt d
             where d.deptno = (select max(d.deptno) from debt d)
            union all
            select d.*
              from debt d
             where d.deptno = (select min(d.deptno) from debt d)            ) loop
  
    pipe row(debt_object(i.DEPTNO, i.dname, i.loc));
  
  end loop debt;

  return;

end function_pipelined;
/

Вывод результата:

select * from table(function_pipelined());

Tags: ,

Comments are closed.