oracle 我需要以表的形式从DBMS_OUTPUT过程中获取输出

f5emj3cl  于 2023-05-22  发布在  Oracle
关注(0)|答案(1)|浏览(227)

我正在使用下面的代码,但我想从dbms_output过程中获得表形式的输出。我可以从下面的代码中以表格的形式得到值吗?

set serveroutput on;

declare
  type t_list is table of zsmart_overall_os%rowtype index by pls_integer;
  zs_smart t_list;
  type e_list is table of mig_mid_bal%rowtype index by pls_integer;
  mig_bal e_list;
begin
  DBMS_OUTPUT.ENABLE(buffer_size=>NULL);
  select * bulk collect into zs_smart from zsmart_overall_os;
  for i in 1..zs_smart.count() loop
    dbms_output.put_line(zs_smart(i).acct_id||'  '||zs_smart(i).os);
  end loop;
end;
guykilcj

guykilcj1#

嗯,你可以-某种程度上-用一些格式。例如:

SQL> DECLARE
  2     TYPE t_list IS TABLE OF emp%ROWTYPE
  3        INDEX BY PLS_INTEGER;
  4
  5     zs_smart  t_list;
  6  BEGIN
  7     SELECT *
  8       BULK COLLECT INTO zs_smart
  9       FROM emp
 10      WHERE deptno = 10;
 11
 12     FOR i IN 1 .. zs_smart.COUNT
 13     LOOP
 14        DBMS_OUTPUT.put_line (
 15              zs_smart (i).empno
 16           || ' '
 17           || RPAD (zs_smart (i).ename, 10, ' ')
 18           || RPAD (zs_smart (i).job, 15, ' ')
 19           || TO_CHAR (zs_smart (i).sal, '999G990D00'));
 20     END LOOP;
 21  END;
 22  /
7782 CLARK     MANAGER           2.450,00
7839 KING      PRESIDENT         5.000,00
7934 MILLER    CLERK             1.300,00

PL/SQL procedure successfully completed.

SQL>

[编辑]

如果它不一定是一个匿名PL/SQL块和DBMS_OUTPUT.PUT_LINE选项,你可以创建一个返回refcursor的函数:

SQL> create or replace function f_test return sys_refcursor is
  2    rc sys_refcursor;
  3  begin
  4    open rc for select * from dept;
  5    return rc;
  6  end;
  7  /

Function created.

SQL> select f_test from dual;

F_TEST
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

还有一种选择是创建一个返回自定义声明类型的函数:

SQL> create or replace type t_dept_row is object
  2    (deptno number,
  3     dname  varchar2(20),
  4     loc    varchar2(20));
  5  /

Type created.

SQL> create or replace type t_dept_tab is table of t_dept_row;
  2  /

Type created.

SQL> create or replace function f_test return t_dept_tab is
  2    l_tab t_dept_tab;
  3  begin
  4    select t_dept_row(deptno, dname, loc)
  5    bulk collect into l_tab from dept;
  6    return l_tab;
  7  end;
  8  /

Function created.

SQL> select * From table(f_test);

    DEPTNO DNAME                LOC
---------- -------------------- --------------------
        10 ACCOUNTING           NEW YORK
        20 RESEARCH             DALLAS
        30 SALES                CHICAGO
        40 OPERATIONS           BOSTON

SQL>

相关问题