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

问题描述 投票:0回答:1

我正在使用下面的代码,但我想以表格的形式从 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;

plsql plsqldeveloper
1个回答
0
投票

好吧,你可以 - 有点 - 使用一些格式。例如:

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>
© www.soinside.com 2019 - 2024. All rights reserved.