Oracle函数简单使用

函数实现的效果是 获取所有职能部门数据并返回

--1创建对象类型,定义对象类型中的属性

create or replace type USER_TYPE_OU_OBJECT as OBJECT(
ou_Id number,
sname varchar2(50),
parent_Id number
);

--2创建USER_TYPE_OU_OBJECT集合类型

create or replace type USER_TYPE_OU_TABLE as Table of USER_TYPE_OU_OBJECT;

--3创建无参数函数

CREATE OR REPLACE FUNCTION F_AAA_GET_OUS return USER_TYPE_OU_TABLE as
--声明部分 声明一个自定义的数据对象(这个是集合)
table_obj USER_TYPE_OU_TABLE;
--主体部分
begin
--初始化集合对象
table_obj := USER_TYPE_OU_TABLE();

for row_d in (select * from T_AAA_OU o where o.type=2)loop
table_obj.extend();--扩展集合大小 不执行会报越界错误
--Dbms_Output.put_line(table_obj.count);
table_obj(table_obj.count) := user_type_ou_object(row_d.ou_id,row_d.sname,row_d.parent_id );
end loop;
Dbms_Output.put_line( table_obj.count());

--再将集合中的数据遍历出来
for i in 1..table_obj.count loop   --遍历集合(collection)的一种方式
if i<50 or (table_obj.count-10<i and i< table_obj.count) then
Dbms_Output.put_line( table_obj(i).ou_id||'-'||table_obj(i).sname);
end if;
end loop;

return table_obj;
end;

原文地址:https://www.cnblogs.com/liuqiang18/p/11843022.html