本文转自:http://www.oraclealchemist.com/oracle/easy-stored-procedure-output/

I answered a question on a DBA Forum today and I thought it was a common enough question to warrant a blog posting.

Question:  I am new to the wonderful world of Oracle. I want to be able to view the results of a stored procedure in an output window, say out of Oracle SQL developer. Unfortunately it appears I need to write some more code to actually view the data.

On a more generic note, can anyone explain to me why Oracle has chosen to make PL/SQL inordinately more complicated than say MS SQL/Servers tSQL? I mean in tSQL I would just write:

CREATE OR REPLACE PROCEDURE TESTSPROC2 AS select * from test_table order by id_no; GO

and viola, a nice result set spits out in Query Analyzer (or a .net application).

Answer:

Before I go on, let me say I agree that PL/SQL is more powerful.  That being said, here are your options.

1. Test it with REFCURSOR using a FUNCTION and selecting from dual:

SQL> create or replace function testfunc return sys_refcursor
2 as
3 c_test sys_refcursor;
4 begin
5 open c_test for select first_name, last_name, email from employees where rownum < 10;
6 return c_test;
7 end;
8 /

Function created.

SQL> select testfunc() from dual;

TESTFUNC()
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

FIRST_NAME LAST_NAME EMAIL
-------------------- ------------------------- -------------------------
Steven King SKING
Neena Kochhar NKOCHHAR
Lex De Haan LDEHAAN
Alexander Hunold AHUNOLD
Bruce Ernst BERNST
David Austin DAUSTIN
Valli Pataballa VPATABAL
Diana Lorentz DLORENTZ
Nancy Greenberg NGREENBE

9 rows selected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
SQL>create orreplace functiontestfunc returnsys_refcursor
  2  as
  3    c_test sys_refcursor;
  4  begin
  5    open c_test forselect first_name,last_name,email from employees where rownum<10;
  6    returnc_test;
  7  end;
  8  /
 
Functioncreated.
 
SQL>select testfunc()from dual;
 
TESTFUNC()
--------------------
CURSOR STATEMENT:1
 
CURSOR STATEMENT:1
 
FIRST_NAME           LAST_NAME                 EMAIL
----------------------------------------------------------------------
Steven               King                      SKING
Neena                Kochhar                   NKOCHHAR
Lex                  De Haan                   LDEHAAN
Alexander            Hunold                    AHUNOLD
Bruce                Ernst                     BERNST
David                Austin                    DAUSTIN
Valli                Pataballa                 VPATABAL
Diana                Lorentz                   DLORENTZ
Nancy                Greenberg                 NGREENBE
 
9rows selected.

2. Use the same function and return it into a variable:

SQL> variable rc refcursor
SQL> exec :rc := testfunc()

PL/SQL procedure successfully completed.

SQL> print rc

FIRST_NAME LAST_NAME EMAIL
-------------------- ------------------------- -------------------------
Steven King SKING
Neena Kochhar NKOCHHAR
Lex De Haan LDEHAAN
Alexander Hunold AHUNOLD
Bruce Ernst BERNST
David Austin DAUSTIN
Valli Pataballa VPATABAL
Diana Lorentz DLORENTZ
Nancy Greenberg NGREENBE

9 rows selected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SQL>variable rc refcursor
SQL>exec:rc:=testfunc()
 
PL/SQL procedure successfully completed.
 
SQL>print rc
 
FIRST_NAME           LAST_NAME                 EMAIL
----------------------------------------------------------------------
Steven               King                      SKING
Neena                Kochhar                   NKOCHHAR
Lex                  De Haan                   LDEHAAN
Alexander            Hunold                    AHUNOLD
Bruce                Ernst                     BERNST
David                Austin                    DAUSTIN
Valli                Pataballa                 VPATABAL
Diana                Lorentz                   DLORENTZ
Nancy                Greenberg                 NGREENBE
 
9rows selected.

3. Use your procedure with a variable:

SQL> create or replace procedure testproc(c_test out sys_refcursor) is
2 begin
3 open c_test for select first_name, last_name, email from employees where rownum < 10;
4 end;
5 /

Procedure created.

SQL> variable rc2 refcursor
SQL> exec testproc(:rc2);

PL/SQL procedure successfully completed.

SQL> print rc2

FIRST_NAME LAST_NAME EMAIL
-------------------- ------------------------- -------------------------
Steven King SKING
Neena Kochhar NKOCHHAR
Lex De Haan LDEHAAN
Alexander Hunold AHUNOLD
Bruce Ernst BERNST
David Austin DAUSTIN
Valli Pataballa VPATABAL
Diana Lorentz DLORENTZ
Nancy Greenberg NGREENBE

9 rows selected.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
SQL>create orreplace procedure testproc(c_test out sys_refcursor)is
  2  begin
  3    open c_test forselect first_name,last_name,email from employees where rownum<10;
  4  end;
  5  /
 
Procedure created.
 
SQL>variable rc2 refcursor
SQL>exec testproc(:rc2);
 
PL/SQL procedure successfully completed.
 
SQL>print rc2
 
FIRST_NAME           LAST_NAME                 EMAIL
----------------------------------------------------------------------
Steven               King                      SKING
Neena                Kochhar                   NKOCHHAR
Lex                  De Haan                   LDEHAAN
Alexander            Hunold                    AHUNOLD
Bruce                Ernst                     BERNST
David                Austin                    DAUSTIN
Valli                Pataballa                 VPATABAL
Diana                Lorentz                   DLORENTZ
Nancy                Greenberg                 NGREENBE
 
9rows selected.

#3 is more in-line with your original needs. Personally I’m a fan of #1 and #2 because of the capabilities of returning a refcursor as a function, like passing it into DBMS_XMLGEN.GETXML.

[转]Easy Stored Procedure Output Oracle Select的更多相关文章

  1. EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework

    原文地址:http://blogs.microsoft.co.il/gilf/2010/05/09/how-to-retrieve-stored-procedure-output-parameters ...

  2. csharp: Oracle Stored Procedure DAL using ODP.NET

    paging : http://www.codeproject.com/Articles/44858/Custom-Paging-GridView-in-ASP-NET-Oracle https:// ...

  3. Oracle Stored Procedure demo

    1.how to find invalid status stored procedure and recompile them? SELECT OBJECT_NAME , status FROM u ...

  4. [转]How to get return values and output values from a stored procedure with EF Core?

    本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a- ...

  5. Dapper: How to get return value ( output value) by call stored procedure

    使用Dapper 执行存储过程插入一条数据,同时返回主键 Dapper 的参数类型有以下四种 System.Data.ParameterDirection public enum ParameterD ...

  6. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

  7. Stored Procedure 里的 WITH RECOMPILE 到底是干麻的?

    在 SQL Server 创建或修改「存储过程(stored procedure)」时,可加上 WITH RECOMPILE 选项,但多数文档或书籍都写得语焉不详,或只解释为「每次执行此存储过程时,都 ...

  8. Difference between Stored Procedure and Function in SQL Server

    Stored Procedures are pre-compile objects which are compiled for first time and its compiled format ...

  9. [转]SSIS: Execute Package via Stored Procedure

    本文转自:http://sqlblog.de/blog/2009/09/ssis-execute-package-via-stored-procedure/ There are two options ...

随机推荐

  1. LIBSVM的使用方法

    [原文:http://wenku.baidu.com/view/7e7b6b896529647d27285276.html] 目  录 1 Libsvm下载... 3 2 Libsvm3.0环境变量设 ...

  2. OpenCV2.3.1中tbb_debug.dll is missing的解决办法

    方法1:        将\opencv\build\common\tbb\ia32目录下的tbb.dll复制改名为tbb_debug.dll  方法2:        若方法1失效,请将\openc ...

  3. 【tyvj1952】easy

    AK大神又AK了!!! orzorzorz 题意: 给出一个字符串由'x'.'o'.'?' '?'有一半的几率为'x' 一半几率为'o' 得分为所有连续的'o'的个数的平方和 如ooxooo 得分为2 ...

  4. Vmware Ubuntu 虚拟机与Windows主机共享文件夹

    概述: 1.安装Vmware tool 2.设置共享文件夹并选择总是连接 3.运行命令挂载共享文件夹 前两步很简单,第三步在root权限下运行命令:mount -t vmhgfs .host:/ /m ...

  5. 【推荐】JavaScript的那些书

    又好久没写东西了,写上一篇的时候还以为接下来的工作会轻松一些,结果未从我所愿呐,又是一阵忙碌.而这段时间穿插着做了很多12年淘宝校园招聘的前端面试,很多同学都有问到,学校里没有前端的课程,那如何学习J ...

  6. A Tour of Go Range continued

    You can skip the index or value by assigning to _. If you only want the index, drop the ", valu ...

  7. Keil C51汉字显示的bug问题

    一.缘起 这两天改进MCU的液晶显示方法,采用“即编即显”的思路,编写了一个可以直接显示字符串的程序.如程序调用disstr("我是你老爸");液晶屏上就会显示“我是你老爸”. 二 ...

  8. SpringMVC(二)

    今天在完成的工作的前提下,最终在睡觉前将SpringMVC和Mybatis整合了~~~ 其实就是按照从网上(参考http://www.toutiao.com/a6332703083554324737/ ...

  9. 【Stage3D学习笔记续】真正的3D世界(六):空间大战

    这就是书上的最终效果了,一个完整的空间大战游戏: 点击查看源码 这里并没有太多的新知识,所涉及的东西更多的是游戏开发方面的优化和技巧,下面我们大家一起来看看: 飞船: 类似粒子效果中的粒子创建方法,我 ...

  10. [BZOJ]2132: 圈地计划 最小割

    圈地计划 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土地是一 ...