函数返回一个rowtype,如何将这个rowtype显示出来?

billlyh 2014-06-06 08:22:46
函数返回一个rowtype,如何将这个rowtype显示出来?
我在某个包中定义了一个函数
function f_get_empname return emp%rowtype is
v_name emp%rowtype;
cursor c1 is
select * from emp ;
;
begin
open c1;
loop
fetch c1 into v_name ;
exit when c1%notfound;
end loop;
return v_name;
exception when others then
if c1%isopen then
close c1;
end if;
dbms_output.put_line('函数异常');
end;
实际的函数比上面复杂得多,这个只是用来表达清楚我的大概意思,
想在调用这个函数f_get_empname,就会返回一个rowtype,
试过这样调用:
declare
v_name emp%rowtype;
begin
--把所有字段都这样赋值
v_name.empno:= f_get_empname.empno ;
v_name.empname:= f_get_empname.empname;
............
dbms_output.put_line('v_name.empname'||v_name.empname);

end;
pl/sql也没报错,但也没有打印出empname
如何把这个函数的返回值显示出来?????
...全文
415 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CT_LXL 2014-06-06
  • 打赏
  • 举报
回复
引用 楼主 billlyh 的回复:
函数返回一个rowtype,如何将这个rowtype显示出来? 我在某个包中定义了一个函数 function f_get_empname return emp%rowtype is v_name emp%rowtype; cursor c1 is select * from emp ; ; begin open c1; loop fetch c1 into v_name ; exit when c1%notfound; end loop; return v_name; exception when others then if c1%isopen then close c1; end if; dbms_output.put_line('函数异常'); end; 实际的函数比上面复杂得多,这个只是用来表达清楚我的大概意思, 想在调用这个函数f_get_empname,就会返回一个rowtype, 试过这样调用: declare v_name emp%rowtype; begin --把所有字段都这样赋值 v_name.empno:= f_get_empname.empno ; v_name.empname:= f_get_empname.empname; ............ dbms_output.put_line('v_name.empname'||v_name.empname); end; pl/sql也没报错,但也没有打印出empname 如何把这个函数的返回值显示出来?????
以下是返回一个结果集(表中所有记录):

create or replace function f_get_empname return sys_refcursor is
  type_cur SYS_REFCURSOR;

begin
  open type_cur for
    select * from scott.emp;
  return type_cur;
end;



declare
  v_ref sys_refcursor;
  rec scott.emp%rowtype;
begin
  v_ref := f_get_empname;
  loop 
    fetch v_ref into rec;
    exit when v_ref%notfound;
  dbms_output.put_line(rec.empno || rec.ename);
  end loop;
end;
CT_LXL 2014-06-06
  • 打赏
  • 举报
回复
引用 楼主 billlyh 的回复:
函数返回一个rowtype,如何将这个rowtype显示出来? 我在某个包中定义了一个函数 function f_get_empname return emp%rowtype is v_name emp%rowtype; cursor c1 is select * from emp ; ; begin open c1; loop fetch c1 into v_name ; exit when c1%notfound; end loop; return v_name; exception when others then if c1%isopen then close c1; end if; dbms_output.put_line('函数异常'); end; 实际的函数比上面复杂得多,这个只是用来表达清楚我的大概意思, 想在调用这个函数f_get_empname,就会返回一个rowtype, 试过这样调用: declare v_name emp%rowtype; begin --把所有字段都这样赋值 v_name.empno:= f_get_empname.empno ; v_name.empname:= f_get_empname.empname; ............ dbms_output.put_line('v_name.empname'||v_name.empname); end; pl/sql也没报错,但也没有打印出empname 如何把这个函数的返回值显示出来?????

其实你这个函数只会返回最后一条记录,因为loop循环完到最后一条记录才return
create or replace function f_get_empname return scott.emp%rowtype is
  v_name scott.emp%rowtype;
  cursor c1 is
    select * from scott.emp;

begin
  open c1;
  loop
    fetch c1
      into v_name;
    exit when c1%notfound;
  end loop;
  return v_name;
exception
  when others then
    if c1%isopen then
      close c1;
    end if;
    dbms_output.put_line('函数异常');
end;

declare
  v_name scott.emp%rowtype;
begin
  --把所有字段都这样赋值
  v_name := f_get_empname;
  dbms_output.put_line(v_name.empno || v_name.ename);
end;

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧