Oracle的存储过程能返回一个select查询结果集吗

panqiang1007 2014-10-20 10:23:12
使用存储过程,想返回一个像下面图的结果集。 这个在PL/SQL里面能实现吗?


因为经常要使用很多联合、多表等查询。而且条件也不同,所以想做个存储过程来保存起来,省得以后每次查询都要重新编写SQL。
刚刚使用Oracle不多久,每次的返回结果都是在输出里面显示,但是在这里面是没有办法对表操作的。

各位大大,这个方法PL/SQL里面能实现吗?
...全文
28097 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiudongxu 2016-11-14
  • 打赏
  • 举报
回复
引用 7 楼 wujunlele 的回复:
[quote=引用 1 楼 zlloct 的回复:] [quote=引用 楼主 panqiang1007 的回复:]
举例:

--创建procedure
create or replace procedure sql_test(out_return out sys_refcursor) is
begin
  open out_return for 'select * from tgp_funds';
end;
2 3 4 5 --创建procedure

--引用

declare
  cur1   SYS_REFCURSOR;
  i      tgp_funds%rowtype;
begin
 sql_test(cur1);
  loop
    fetch cur1
      into i;
    exit when cur1%notfound;
    dbms_output.put_line('----------------:' || i.fnd_id);--fnd_id为表tgp_funds中的fnd_id 列
  end loop;
  close cur1;
end;
[/quote] 能问一下吗 提供了下面两个代码,如果在存储过程里只用--创建procedure 的代码,会返回结果集吗,还是--创建procedure --引用两个一起用才能返回结果集? 谢谢了 --创建procedure --引用[/quote] 一个是创建的语句,一个是调用的。 过程是先创建好了,这个存储过程一直放在那存着, 你想什么时候用,就什么时候调用,也就是上文说的引用的代码。
lelewu125 2016-10-21
  • 打赏
  • 举报
回复
引用 1 楼 zlloct 的回复:
[quote=引用 楼主 panqiang1007 的回复:]
举例:

--创建procedure
create or replace procedure sql_test(out_return out sys_refcursor) is
begin
  open out_return for 'select * from tgp_funds';
end;
2 3 4 5 --创建procedure

--引用

declare
  cur1   SYS_REFCURSOR;
  i      tgp_funds%rowtype;
begin
 sql_test(cur1);
  loop
    fetch cur1
      into i;
    exit when cur1%notfound;
    dbms_output.put_line('----------------:' || i.fnd_id);--fnd_id为表tgp_funds中的fnd_id 列
  end loop;
  close cur1;
end;
[/quote] 能问一下吗 提供了下面两个代码,如果在存储过程里只用--创建procedure 的代码,会返回结果集吗,还是--创建procedure --引用两个一起用才能返回结果集? 谢谢了 --创建procedure --引用
panqiang1007 2014-10-21
  • 打赏
  • 举报
回复
好吧,貌似也只能这样了。 TKS!
CT_LXL 2014-10-20
  • 打赏
  • 举报
回复
引用 3 楼 panqiang1007 的回复:
你可以将你的SQL通过建立视图保存起来,以后每次查询直接查询视图,这样能够达到你想要的效果。也可以避免你每次去重复的编写复杂的SQL
bw555 2014-10-20
  • 打赏
  • 举报
回复
这个难了吧,貌似不可以
panqiang1007 2014-10-20
  • 打赏
  • 举报
回复
引用
引用 1 楼 zlloct 的回复:


按照你的代码查询的结果还是在输出里面显示的呀。


我想要的结果是 执行存储过程显示的结果在SQL里面的。

是像这样的结果。 能实现吗?

bw555 2014-10-20
  • 打赏
  • 举报
回复
引用 1 楼 zlloct 的回复:
[quote=引用 楼主 panqiang1007 的回复:]
举例:

--创建procedure
create or replace procedure sql_test(out_return out sys_refcursor) is
begin
  open out_return for 'select * from tgp_funds';
end;

--引用

declare
  cur1   SYS_REFCURSOR;
  i      tgp_funds%rowtype;
begin
 sql_test(cur1);
  loop
    fetch cur1
      into i;
    exit when cur1%notfound;
    dbms_output.put_line('----------------:' || i.fnd_id);--fnd_id为表tgp_funds中的fnd_id 列
  end loop;
  close cur1;
end;
[/quote] 正解++
CT_LXL 2014-10-20
  • 打赏
  • 举报
回复
引用 楼主 panqiang1007 的回复:
举例:

--创建procedure
create or replace procedure sql_test(out_return out sys_refcursor) is
begin
  open out_return for 'select * from tgp_funds';
end;

--引用

declare
  cur1   SYS_REFCURSOR;
  i      tgp_funds%rowtype;
begin
 sql_test(cur1);
  loop
    fetch cur1
      into i;
    exit when cur1%notfound;
    dbms_output.put_line('----------------:' || i.fnd_id);--fnd_id为表tgp_funds中的fnd_id 列
  end loop;
  close cur1;
end;

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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