如何消除PL/SQL存储过程输出中的异常方块?
Ah, I see the issue here! Those weird squares in your output are caused by the chr(18) you're using as a separator. Let me break this down for you:
chr(18) is a non-printable control character called "Cancel Escape" — terminal emulators can't render it properly, so they display those placeholder squares instead. To fix this, you just need to replace chr(18) with a visible, printable separator. Here are a couple of clean options:
Option 1: Clean Aligned Output with Delimiters
My recommendation for human-readable results: move the header outside the loop (so it doesn't repeat for every row) and use RPAD to align columns neatly with a | separator. This will give you a clean table-like appearance:
create or replace procedure numberOfSupplier (x int := 0 ) is begin -- Print header once before the loop dbms_output.put_line (RPAD('R_NAME', 15) || ' | ' || RPAD('N_NAME', 15) || ' | ' || 'COUNT(S_NATIONKEY)'); dbms_output.put_line('----------------------------------------------'); for QRow in (SELECT REGION.R_NAME,NATION.N_NAME,COUNT(SUPPLIER.S_NATIONKEY) counter FROM REGION INNER JOIN NATION ON NATION.N_REGIONKEY=REGION.R_REGIONKEY INNER JOIN SUPPLIER ON SUPPLIER.S_NATIONKEY=NATION.N_NATIONKEY GROUP BY SUPPLIER.S_NATIONKEY,REGION.R_NAME,NATION.N_NAME HAVING COUNT(SUPPLIER.S_NATIONKEY)> x) loop dbms_output.put_line (RPAD(QRow.R_NAME, 15) || ' | ' || RPAD(QRow.N_NAME, 15) || ' | ' || QRow.counter); end loop; end; / show errors; execute numberOfSupplier(130);
Option 2: Tab-Separated Output
If you prefer a compact, spreadsheet-friendly layout, replace chr(18) with chr(9) (the ASCII tab character):
create or replace procedure numberOfSupplier (x int := 0 ) is begin -- Print header once dbms_output.put_line ('R_NAME' || chr(9) || 'N_NAME' || chr(9) || 'COUNT(S_NATIONKEY)'); for QRow in (SELECT REGION.R_NAME,NATION.N_NAME,COUNT(SUPPLIER.S_NATIONKEY) counter FROM REGION INNER JOIN NATION ON NATION.N_REGIONKEY=REGION.R_REGIONKEY INNER JOIN SUPPLIER ON SUPPLIER.S_NATIONKEY=NATION.N_NATIONKEY GROUP BY SUPPLIER.S_NATIONKEY,REGION.R_NAME,NATION.N_NAME HAVING COUNT(SUPPLIER.S_NATIONKEY)> x) loop dbms_output.put_line (QRow.R_NAME || chr(9) || QRow.N_NAME || chr(9) || QRow.counter); end loop; end; / show errors; execute numberOfSupplier(130);
Quick Note:
Also, I removed the extra chr(10) from your code — dbms_output.put_line automatically adds a newline at the end, so that was just creating unnecessary blank lines in your output.
Either of these changes will eliminate those annoying squares and give you the clean output you're looking for!
内容的提问来源于stack exchange,提问作者Chan Myae Tun

