简体   繁体   中英

How to properly output PL/SQL

I have two blocks of code inside a .sql file. 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. Then the procedure has another query which needs to be printed on the same line (I'm using 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.

Here's a link to the code: http://pastebin.com/z29emmBJ (The relevant part of the code is around lines: 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.

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

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? 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). 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(); inside the loop after DBMS_OUTPUT.PUT you just emulating DBMS_OUTPUT.PUT_LINE procedure.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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