oracle 如何在同一过程中返回多个选择查询

3lxsmp7m  于 2023-02-15  发布在  Oracle
关注(0)|答案(2)|浏览(151)

我想问一下如何打印过程中多条语句的输出。假设你想显示dba_objects和段行计数。但是我不能使用dbms_sql. return_result我的版本是11g。
就像,

create or replace procedure get_rows_count
(
  cursor1 out SYS_REFCURSOR,
  cursor2 out SYS_REFCURSOR
)
as
begin
    open cursor1 for select count(*) from dba_objects;
    open cursor2 for select count(*) from dba_segments;
end get_rows_count;
/
myss37ts

myss37ts1#

假设您要显示dba_objects和段行计数
我猜的。结论:这不是实现它的方法。如果你想从dba_objects中获取行计数,那么你应该

select count(*) from dba_objects;

在任何您想要的变体中(纯SQL、返回那个数字的函数、带有OUT参数的过程(更糟糕的选择),...)。但是,创建一个使用引用游标来那个目的的过程是...嗯,是 * 错误的 *。
如果我理解错了,那么:您编写的过程是可以的。您可以从另一个PL/SQL过程(命名或匿名)调用它,将结果提取到变量中,并对它执行某些操作(例如显示它)。
您的程序(从Scott的表格中选择;我没有访问DBA_ views的权限):

SQL> CREATE OR REPLACE PROCEDURE get_rows_count (cursor1  OUT SYS_REFCURSOR,
  2                                              cursor2  OUT SYS_REFCURSOR)
  3  AS
  4  BEGIN
  5     OPEN cursor1 FOR SELECT * FROM emp;
  6
  7     OPEN cursor2 FOR SELECT * FROM dept;
  8  END get_rows_count;
  9  /

Procedure created.

怎么称呼它?见第8行:

SQL> SET SERVEROUTPUT ON
SQL>
SQL> DECLARE
  2     rc1  SYS_REFCURSOR;
  3     rc2  SYS_REFCURSOR;
  4     --
  5     rw1  emp%ROWTYPE;
  6     rw2  dept%ROWTYPE;
  7  BEGIN
  8     get_rows_count (rc1, rc2);
  9
 10     DBMS_OUTPUT.put_line ('Employees -----------');
 11
 12     LOOP
 13        FETCH rc1 INTO rw1;
 14
 15        EXIT WHEN rc1%NOTFOUND;
 16
 17        DBMS_OUTPUT.put_line (rw1.ename);
 18     END LOOP;
 19
 20     --
 21     DBMS_OUTPUT.put_line ('Departments ---------');
 22
 23     LOOP
 24        FETCH rc2 INTO rw2;
 25
 26        EXIT WHEN rc2%NOTFOUND;
 27
 28        DBMS_OUTPUT.put_line (rw2.dname);
 29     END LOOP;
 30
 31     DBMS_OUTPUT.put_line ('First ref cursor: ' || rc1%ROWCOUNT);
 32     DBMS_OUTPUT.put_line ('Second ref cursor: ' || rc2%ROWCOUNT);
 33  END;
 34  /

结果:

Employees -----------
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
Departments ---------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
First ref cursor: 14
Second ref cursor: 4

PL/SQL procedure successfully completed.

SQL>
nle07wnf

nle07wnf2#

您可以将著名的DBMS_OUTPUT.PUT_LINE()%ROWCOUNT后缀沿着用于您的情况,例如

SET serveroutput ON
CREATE OR REPLACE PROCEDURE get_rows_count(
                                            cursor1 OUT SYS_REFCURSOR,
                                            cursor2 OUT SYS_REFCURSOR,
                                            count1  OUT INT,
                                            count2  OUT INT
                                          ) AS
  cur_rec_obj user_objects%ROWTYPE;
  cur_rec_seg user_segments%ROWTYPE;      
BEGIN
    OPEN cursor1 FOR SELECT * FROM user_objects;
    LOOP
      FETCH cursor1 INTO cur_rec_obj;  
      EXIT WHEN cursor1%NOTFOUND;
    END LOOP;

    OPEN cursor2 FOR SELECT * FROM user_segments;
    LOOP
      FETCH cursor2 INTO cur_rec_seg;  
      EXIT WHEN cursor2%NOTFOUND;
    END LOOP;

    count1 := cursor1%ROWCOUNT;
    count2 := cursor2%ROWCOUNT;
    DBMS_OUTPUT.PUT_LINE(count1);
    DBMS_OUTPUT.PUT_LINE(count2);    
      
END;
/

并且可以从PL/SQL Developer的SQL窗口中按如下方式调用:

DECLARE
 v_cursor1   SYS_REFCURSOR;
 v_cursor2   SYS_REFCURSOR;
 v_count1    INT;
 v_count2    INT;
BEGIN
  get_rows_count(v_cursor1, v_cursor2, v_count1, v_count2 );
END;
/

相关问题