如何在 pl sql 中使用匿名块打印整个表?

发布于 2024-08-22 19:54:52 字数 93 浏览 5 评论 0原文

我想使用DBMS_OUTPUT.PUT_LINE,但行数仅超过1。表中没有任何内容按顺序递增,因此我无法使用循环。有没有办法打印出表格中的每一行?

I want to use DBMS_OUTPUT.PUT_LINE, but the number of rows exceeds just 1. There is nothing in the table which increments sequentially, so I can't use a loop. Is there some way to print out each row in the table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女情怀诗 2024-08-29 19:54:52

尝试这样的事情。

SET SERVEROUTPUT ON
     BEGIN
          -- A PL/SQL cursor
          FOR cursor1 IN (SELECT * FROM table1) 
          LOOP
            DBMS_OUTPUT.PUT_LINE('Column 1 = ' || cursor1.column1 ||
                               ', Column 2 = ' || cursor1.column2);
          END LOOP;
     END;
        /

try with something like this.

SET SERVEROUTPUT ON
     BEGIN
          -- A PL/SQL cursor
          FOR cursor1 IN (SELECT * FROM table1) 
          LOOP
            DBMS_OUTPUT.PUT_LINE('Column 1 = ' || cursor1.column1 ||
                               ', Column 2 = ' || cursor1.column2);
          END LOOP;
     END;
        /
三月梨花 2024-08-29 19:54:52

这可能会有所帮助:

BEGIN    
  FOR MY_CURSOR IN (SELECT COLUMN1,COLUMN2,COLUMN3 FROM MY_TABLE) 
  LOOP
    DBMS_OUTPUT.PUT_LINE('COLUMN1 = ' || MY_CURSOR.COLUMN1 ||', 
                          COLUMN2 = ' || MY_CURSOR.COLUMN2 ||',
                          COLUMN3 = ' || MY_CURSOR.COLUMN3);
  END LOOP;
END;

This may help:

BEGIN    
  FOR MY_CURSOR IN (SELECT COLUMN1,COLUMN2,COLUMN3 FROM MY_TABLE) 
  LOOP
    DBMS_OUTPUT.PUT_LINE('COLUMN1 = ' || MY_CURSOR.COLUMN1 ||', 
                          COLUMN2 = ' || MY_CURSOR.COLUMN2 ||',
                          COLUMN3 = ' || MY_CURSOR.COLUMN3);
  END LOOP;
END;
梦途 2024-08-29 19:54:52

做到这一点的快速但最肮脏的方法实际上是通过 SQL*Plus:

SQL>  set lines 200
SQL>  set heading off
SQL>  set feedback off
SQL>  spool $HOME/your_table.out
SQL>  select * from your_table;
SQL>  spool off

SQL*Plus 有一些简洁但基本的报告功能;我们甚至可以生成 HTML 文件

如果您有一个非常长的表(许多行)或一个宽表(许多列),您最好直接输出到文件,就像这样。

declare
    fh utl_file.file_type;
begin
    fh := utl_file.fopen('TARGET_DIRECTORY', 'your_table.lst', 'W');
    for lrec in ( select * from your_table )
    loop
        utl_file.put( fh, id );         
        utl_file.put( fh, '::' );         
        utl_file.put( fh, col_1 );         
        utl_file.put( fh, '::' );         
        utl_file.put( fh, col_2 );         
        utl_file.put( fh, '::' );         
        utl_file.put( fh, to_char ( col_3, 'dd-mm-yyyy hh24:mi:ss' ) );         
        utl_file.new_line(fh);         
    end loop;
    utl_file.fclose(fh);
end; 
/

这可能看起来很麻烦,但 PUT() 调用可以从 USER_TAB_COLUMNS 生成。 UTL_FILE 有一些问题,因此 阅读文档

可以对 DBMS_OUTPUT 使用相同的控制结构......

begin
    for lrec in ( select * from your_table )
    loop
        dbms_output.put( id );         
        dbms_output.put( '::' );         
        dbms_output.put( col_1 );         
        dbms_output.put( '::' );         
        dbms_output.put( col_2 );         
        dbms_output.put( '::' );         
        dbms_output.put( to_char ( col_3, 'dd-mm-yyyy hh24:mi:ss' ) );         
        dbms_output.new_line;         
    end loop;
end;
/

但是如果您要从 SQL*Plus 中假脱机输出,为什么不使用更简单的选项呢?

The quick and dirtiest way of doing this is actually through SQL*Plus:

SQL>  set lines 200
SQL>  set heading off
SQL>  set feedback off
SQL>  spool $HOME/your_table.out
SQL>  select * from your_table;
SQL>  spool off

SQL*Plus has some neat if basic reporting functionality; we can even generate HTML files.

If you have a very long table (many rows) or a wide one (many columns) you may be better off outputting directly to a file, like this.

declare
    fh utl_file.file_type;
begin
    fh := utl_file.fopen('TARGET_DIRECTORY', 'your_table.lst', 'W');
    for lrec in ( select * from your_table )
    loop
        utl_file.put( fh, id );         
        utl_file.put( fh, '::' );         
        utl_file.put( fh, col_1 );         
        utl_file.put( fh, '::' );         
        utl_file.put( fh, col_2 );         
        utl_file.put( fh, '::' );         
        utl_file.put( fh, to_char ( col_3, 'dd-mm-yyyy hh24:mi:ss' ) );         
        utl_file.new_line(fh);         
    end loop;
    utl_file.fclose(fh);
end; 
/

This may look like a chore, but the PUT() calls can be generated from USER_TAB_COLUMNS. There are a couple of gotchas with UTL_FILE so read the documentation.

You could use the same control structure with DBMS_OUTPUT....

begin
    for lrec in ( select * from your_table )
    loop
        dbms_output.put( id );         
        dbms_output.put( '::' );         
        dbms_output.put( col_1 );         
        dbms_output.put( '::' );         
        dbms_output.put( col_2 );         
        dbms_output.put( '::' );         
        dbms_output.put( to_char ( col_3, 'dd-mm-yyyy hh24:mi:ss' ) );         
        dbms_output.new_line;         
    end loop;
end;
/

... but if you are going to spool out from a SQL*Plus, why not use the easier option?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文