简体   繁体   English

如何正确输出PL / SQL

[英]How to properly output PL/SQL

I have two blocks of code inside a .sql file. 我在.sql文件中有两个代码块。 One block is a function and another is a procedure. 一个块是功能,另一个是过程。 In the first block, I'm running a query and printing it out to the screen (I'm using DBMS_OUTPUT.PUT_LINE() ) for each row in it's own line. 在第一个块中,我正在运行查询并将其自己的每一行打印到屏幕上(我正在使用DBMS_OUTPUT.PUT_LINE())。 Then the procedure has another query which needs to be printed on the same line (I'm using DBMS_OUTPUT.PUT() ). 然后,该过程还有另一个查询,需要在同一行上打印(我正在使用DBMS_OUTPUT.PUT())。 When I use DBMS_OUTPUT.PUT() for the second block, it screws up the first block for some reason and the first block never prints. 当我对第二个块使用DBMS_OUTPUT.PUT()时,由于某种原因,它拧紧了第一个块,并且第一个块从不打印。

Here's a link to the code: http://pastebin.com/z29emmBJ (The relevant part of the code is around lines: 97-103) 这是代码的链接: http : //pastebin.com/z29emmBJ (代码的相关部分在以下几行中:97-103)

When I have DBMS_OUTPUT.PUT_LINE() being used inside of the procedure, everything displays properly, but when I have DBMS_OUTPUT.PUT() inside of the procedure, it looks like the function never gets called. 当我在过程内部使用DBMS_OUTPUT.PUT_LINE()时,一切都可以正确显示,但是当我在过程内部使用DBMS_OUTPUT.PUT()时,似乎该函数从未被调用。

Here's what the output looks like with PUT_LINE(): http://i.imgur.com/AnCv9.png Here's what the output looks like with just PUT(): http://i.imgur.com/Jv3SV.png 这是使用PUT_LINE()的输出结果: http : //i.imgur.com/AnCv9.png这是仅使用PUT()的输出结果: http : //i.imgur.com/Jv3SV.png

I think it has something to do with the buffer size, but I'm not exactly sure what/why. 我认为这与缓冲区大小有关,但是我不确定是什么原因。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Why don't you just append the results to a VARCHAR2 variable as needed, then put_line that string when the row is completed? 您为什么不只根据需要将结果附加到VARCHAR2变量,然后在完成行后将结果字符串放到该字符串上? That way you have control over the formatting. 这样,您就可以控制格式。

Snippet of the code of your Second stored procedure: 第二个存储过程的代码片段:

FOR player IN rows LOOP
   currentCount := maxCount;
   DBMS_OUTPUT.PUT(player.FIRSTNAME || ' ' || player.LASTNAME || ':' || player.points || ' ');
   --DBMS_OUTPUT.NEW_LINE();
END LOOP;

If you want that the resulting output appeared as a one line you should move DBMS_OUTPUT.NEW_LINE() outside the loop (after the loop). 如果希望结果输出显示为一行,则应将DBMS_OUTPUT.NEW_LINE()移到循环外(循环之后)。 So your code would look like: 因此您的代码如下所示:

FOR player IN rows LOOP
  currentCount := maxCount;
  DBMS_OUTPUT.PUT(player.FIRSTNAME || ' ' || player.LASTNAME || ':' || player.points || ' ');         
END LOOP;
DBMS_OUTPUT.NEW_LINE();

Keeping DBMS_OUTPUT.NEW_LINE(); 保留DBMS_OUTPUT.NEW_LINE(); inside the loop after DBMS_OUTPUT.PUT you just emulating DBMS_OUTPUT.PUT_LINE procedure. DBMS_OUTPUT.PUT之后的循环内,您仅模拟DBMS_OUTPUT.PUT_LINE过程。

SQL> create or replace procedure output1
  2  is
  3    l_str varchar2(100);
  4    l_status number;
  5  begin
  6    for i in 1..7
  7    loop
  8      dbms_output.put('Text_' || To_char(i));
  9      dbms_output.new_line;
 10    end loop;
 11  end;
 12  /

Procedure created

SQL> 
SQL> create or replace procedure output2
  2  is
  3    l_str varchar2(100);
  4    l_status number;
  5  begin
  6    for i in 1..7
  7    loop
  8      dbms_output.put('Text_' || To_char(i));
  9     end loop;
 10     dbms_output.new_line;
 11  end;
 12  /

Procedure created

SQL> exec output1;

Text_1
Text_2
Text_3
Text_4
Text_5
Text_6
Text_7

PL/SQL procedure successfully completed

SQL> exec output2;

Text_1Text_2Text_3Text_4Text_5Text_6Text_7

PL/SQL procedure successfully completed

In your code: 在您的代码中:

SET serveroutput ON size 32000;

REM Change output file name TO proj3-NetID.OUT!
SPOOL proj3-hgeorge3.OUT;
exec DBMS_OUTPUT.enable('100000000');

If serveroutput option is used (set to ON) then there is no need of calling DBMS_OUTPUT.enable procedure. 如果使用serveroutput选项(设置为ON),则无需调用DBMS_OUTPUT.enable过程。 And if it happens to call DBMS_OUTPUT.enable then the value of numeric data type should be passed in as a parameter not a string. 如果碰巧调用DBMS_OUTPUT.enable ,则应将数字数据类型的值作为参数而不是字符串传递。 Yes there will be implicit conversion of data types but it's better to avoid it. 是的,将进行数据类型的隐式转换,但最好避免这种转换。 And maximum size of the buffer is 1 million. 缓冲区的最大大小为一百万。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM