oracle 通过function 函数 返回结果集

本文介绍了一个PL/SQL函数的实现过程,该函数通过创建数据对象、类表对象及函数来获取房间信息。首先定义了包含房间ID、面积等属性的数据对象,然后创建了用于存放查询结果的类表对象,并最终实现了返回房间信息集合的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己理解分三步走

1.创建数据对象,用于保存结果集中的结果。

create or replace type room as object
(
  roomid varchar2(12),
  roomarea varchar2(12),
  kogicode varchar2(10),
  structureid varchar2(12),
  campusid varchar2(12),
  isoffice varchar2(1)
);

2.创建类表对象,用于保存要返回的结果。

create or replace type roomtable is table of room

3.创建函数。

create or replace function get511(iyear in number ) --get511为函数名称,iyear为输入参数
  return roomtable --返回表
  as
  --定义变量
  roomobj room; 
  itable roomtable  := roomtable();
  xtyear varchar(4) :=to_char(sysdate,'yyyy');
   cursor newtestrooms is
       select sri.room_id,sri.room_area,srui.kogi_code,sri.structures_id,ssi.campus_id,srui.use_isoffice from sf_roominfo sri ,sf_structuresinfo ssi,sf_roomuseinfo srui where srui.room_id=sri.room_id and ssi.structures_id=sri.structures_id and ssi.structures_state <> 6 and ssi.structures_state <> 1;
   cursor hisrooms is 
          select t.room_id from sf_roominfo t ;
begin
  --判断输入的年份
  if xtyear <= iyear
    then
      --用户输入的年份为当前年份或者之后
  for cur in newtestrooms loop
     roomobj :=room(cur.room_id, cur.room_area ,cur.kogi_code,cur.structures_id,cur.campus_id,cur.use_isoffice);
    itable.extend();
    itable(itable.count) := roomobj;
   end loop;
  return itable;
  end if;

  if xtyear > iyear
    then
      --用户输入的年份为当前年份之前
   for hcur in hisrooms loop
     
    roomobj :=room(i, i*i,xtyear ,'小于',i+i,'1');
    itable.extend();
    itable(itable.count) := roomobj;
  end loop;
  return itable;
  end if;
end get511;

以上函数没有写完,需要自己这边的表,不能直接调用。

要调用的话直接

select * from table(get511(2018))
OK。大功告成



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值