Oracle的函数function

本文介绍了一个使用PL/SQL编写的动态游标函数,该函数可以根据输入参数选择性地从EMP表中检索不同部门的数据。通过创建或替换一个名为FUN_EMP的函数,实现了根据用户指定的部门编号返回相应部门员工记录的功能。此外,还展示了如何声明并使用SYS_REFCURSOR类型的游标来迭代查询结果。

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

create or replace function fun_emp(prm_choice in number)
          return sys_refcursor
          is
                 empcursor sys_refcursor;
          begin
            if prm_choice=1 then
              open empcursor for select * from emp where deptno=10;
            elsif prm_choice=2 then
               open empcursor for select * from emp where deptno=20;
             elsif prm_choice=3 then
               open empcursor for select * from emp where deptno=30;  
              else
                open empcursor  for select  * from emp ;
               end if; 
                return empcursor;
          end;
          --调用
          declare 
                empcursor sys_refcursor;
                rec_emp emp%Rowtype;
                begin
                  empcursor :=fun_emp(10);
                       if empcursor%isopen then
                         loop
                           fetch empcursor into rec_emp;
                           exit when empcursor%notfound;
                           dbms_output.put_line(rec_emp.ename);
                         end loop;
                       end if;
                       close empcursor;  
                end;
          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值