<small id='2Eipo'></small><noframes id='2Eipo'>

    1. <legend id='2Eipo'><style id='2Eipo'><dir id='2Eipo'><q id='2Eipo'></q></dir></style></legend>

    2. <tfoot id='2Eipo'></tfoot>
      <i id='2Eipo'><tr id='2Eipo'><dt id='2Eipo'><q id='2Eipo'><span id='2Eipo'><b id='2Eipo'><form id='2Eipo'><ins id='2Eipo'></ins><ul id='2Eipo'></ul><sub id='2Eipo'></sub></form><legend id='2Eipo'></legend><bdo id='2Eipo'><pre id='2Eipo'><center id='2Eipo'></center></pre></bdo></b><th id='2Eipo'></th></span></q></dt></tr></i><div id='2Eipo'><tfoot id='2Eipo'></tfoot><dl id='2Eipo'><fieldset id='2Eipo'></fieldset></dl></div>

        • <bdo id='2Eipo'></bdo><ul id='2Eipo'></ul>

        如何从返回引用游标的 Oracle 过程中获得格式良好的结果?

        时间:2023-09-20
        <legend id='ISo87'><style id='ISo87'><dir id='ISo87'><q id='ISo87'></q></dir></style></legend>

        <small id='ISo87'></small><noframes id='ISo87'>

        <tfoot id='ISo87'></tfoot>
            <tbody id='ISo87'></tbody>
        • <i id='ISo87'><tr id='ISo87'><dt id='ISo87'><q id='ISo87'><span id='ISo87'><b id='ISo87'><form id='ISo87'><ins id='ISo87'></ins><ul id='ISo87'></ul><sub id='ISo87'></sub></form><legend id='ISo87'></legend><bdo id='ISo87'><pre id='ISo87'><center id='ISo87'></center></pre></bdo></b><th id='ISo87'></th></span></q></dt></tr></i><div id='ISo87'><tfoot id='ISo87'></tfoot><dl id='ISo87'><fieldset id='ISo87'></fieldset></dl></div>

                  <bdo id='ISo87'></bdo><ul id='ISo87'></ul>

                  本文介绍了如何从返回引用游标的 Oracle 过程中获得格式良好的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 MS SQL Server 中,如果我想检查存储过程的结果,我可能会在 Management Studio 中执行以下操作.

                  In MS SQL Server if I want to check the results from a Stored procedure I might execute the following in Management Studio.

                  --SQL SERVER WAY
                  exec sp_GetQuestions('OMG Ponies')
                  

                  结果窗格中的输出可能如下所示.

                  The output in the results pane might look like this.

                  ID    Title                                             ViewCount   Votes 
                  ----- ------------------------------------------------- ---------- --------
                  2165  Indexed View vs Indexes on Table                  491         2  
                  5068  SQL Server equivalent to Oracle’s NULLS FIRST     524         3 
                  1261  Benefits Of Using SQL Ordinal Position Notation?  377         2 
                  
                  (3 row(s) affected)
                  

                  无需编写循环或 PRINT 语句.

                  No need to write loops or PRINT statements.

                  为了在 Oracle 中做同样的事情,我可能会在 SQL Developer 中执行以下匿名块

                  To do the same thing in Oracle I might execute the following anonymous block in SQL Developer

                  --ORACLE WAY
                      DECLARE
                          OUTPUT  MYPACKAGE.refcur_question;
                          R_OUTPUT MYPACKAGE.r_question;
                          USER    VARCHAR2(20);
                  
                  BEGIN
                  
                    dbms_output.enable(10000000);
                    USER:= 'OMG Ponies';
                    recordCount := 0;
                  
                  
                  
                    MYPACKAGE.GETQUESTIONS(p_OUTPUT => OUTPUT, 
                    p_USER=> USER, 
                  
                    ) ;
                  
                  
                  
                  
                    DBMS_OUTPUT.PUT_LINE('ID |  Title | ViewCount | Votes' );
                  
                    LOOP 
                      FETCH OUTPUT
                      INTO R_OUTPUT;
                  
                           DBMS_OUTPUT.PUT_LINE(R_OUTPUT.QUESTIONID || '|' || R_OUTPUT.TITLE 
                                 '|' || R_OUTPUT.VIEWCOUNT '|' || R_OUTPUT.VOTES);
                            recordCount := recordCount+1;
                  
                  
                  
                  
                   EXIT WHEN OUTPUT % NOTFOUND;  
                        END LOOP;
                        DBMS_OUTPUT.PUT_LINE('Record Count:'||recordCount);
                        CLOSE OUTPUT;
                  
                  
                      END;
                  

                  这个输出像

                  ID|Title|ViewCount|Votes 
                  2165|Indexed View vs Indexes on Table|491|2  
                  5068|SQL Server equivalent to Oracle’s NULLS FIRST|524|3 
                  1261|Benefits Of Using SQL Ordinal Position Notation?|377|2 
                  Record Count: 3
                  

                  所以SQL版本有1行,oracle有18行,输出难看.如果有很多列和/或数据是数字,它会加剧.

                  So the SQL version has 1 line and the oracle has 18 and the output is ugly. Its exacerbated if there are a lot of columns and/or the data is numeric.

                  令我感到奇怪的是,如果我在 SQL Developer 或 Management Studio 中编写此语句...

                  What's odd to me about this is that if I write this statement in either SQL Developer or Management studio...

                  SELECT 
                  ID, 
                  Title, 
                  ViewCount, 
                  Votes
                  FROM votes where user = 'OMG Ponies'  
                  

                  结果非常相似.这让我觉得我要么错过了一项技术,要么使用了错误的工具.

                  The results are fairly similar. This makes me feel like I'm either missing a technique or using the wrong tool.

                  推荐答案

                  如果 GetQuestions 是一个返回 refcursor 的函数,这似乎是您在 SQL Server 版本中所拥有的,那么您可能能够做这样的事情:

                  If GetQuestions is a function returning a refcursor, which seems to be what you have in the SQL Server version, then rather you may be able to do something like this:

                  select * from table(MyPackage.GetQuestions('OMG Ponies'));
                  

                  或者,如果您在 PL/SQL 块中需要它,那么您可以在游标中使用相同的选择.

                  Or if you need it in a PL/SQL block then you can use the same select in a cursor.

                  您也可以让函数生成 dbms_output 语句,以便它们始终可用于调试,尽管这会增加一些开销.

                  You can also have the function produce the dbms_output statements instead so they're always available for debugging, although that adds a little overhead.

                  编辑

                  嗯,不确定是否可以将返回的 refcursor cast() 转换为可用类型,除非您愿意在包外声明自己的类型(以及该类型的表).你可以这样做,只是为了转储结果:

                  Hmmm, not sure it's possible to cast() the returned refcursor to a usable type, unless you're willing to declare your own type (and a table of that type) outside the package. You can do this though, just to dump the results:

                  create package mypackage as
                      function getquestions(user in varchar2) return sys_refcursor;
                  end mypackage;
                  /
                  
                  create package body mypackage as
                      function getquestions(user in varchar2) return sys_refcursor as
                          r sys_refcursor;
                      begin
                          open r for
                              /* Whatever your real query is */
                              select 'Row 1' col1, 'Value 1' col2 from dual
                              union
                              select 'Row 2', 'Value 2' from dual
                              union
                              select 'Row 3', 'Value 3' from dual;
                              return r;
                      end;
                  end mypackage;
                  /
                  
                  var r refcursor;
                  exec :r := mypackage.getquestions('OMG Ponies');
                  print r;
                  

                  并且您可以在另一个过程或函数中使用调用的结果;它只是在 PL/SQL 之外进行,这似乎有点棘手.

                  And you can use the result of the call in another procedure or function; it's just getting to it outside PL/SQL that seems to be a little tricky.

                  编辑添加:使用这种方法,如果它是一个程序,您可以做基本相同的事情:

                  Edited to add: With this approach, if it's a procedure you can do essentially the same thing:

                  var r refcursor;
                  exec mypackage.getquestions(:r, 'OMG Ponies');
                  print r;
                  

                  这篇关于如何从返回引用游标的 Oracle 过程中获得格式良好的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Sql*plus 总是返回退出代码 0? 下一篇:为什么使用 JOIN 子句而不是 WHERE 条件?

                  相关文章

                    <bdo id='duD2g'></bdo><ul id='duD2g'></ul>
                  <tfoot id='duD2g'></tfoot>

                      <i id='duD2g'><tr id='duD2g'><dt id='duD2g'><q id='duD2g'><span id='duD2g'><b id='duD2g'><form id='duD2g'><ins id='duD2g'></ins><ul id='duD2g'></ul><sub id='duD2g'></sub></form><legend id='duD2g'></legend><bdo id='duD2g'><pre id='duD2g'><center id='duD2g'></center></pre></bdo></b><th id='duD2g'></th></span></q></dt></tr></i><div id='duD2g'><tfoot id='duD2g'></tfoot><dl id='duD2g'><fieldset id='duD2g'></fieldset></dl></div>

                    1. <legend id='duD2g'><style id='duD2g'><dir id='duD2g'><q id='duD2g'></q></dir></style></legend>

                    2. <small id='duD2g'></small><noframes id='duD2g'>