oracle 如何将 PL/SQL 中的输出格式化为直列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16095032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format my output in PL/SQL into straight columns?
提问by DaBulls33
I am wondering how I can get my output of the following code to be displayed in a neat column. The output now is not neatly organized. Is there something I need to put in front of my variable?
我想知道如何将以下代码的输出显示在一个整齐的列中。现在的输出没有整齐地组织。有什么我需要放在我的变量前面的吗?
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || (v_name));
DBMS_OUTPUT.PUT_LINE('Job: ' || (v_job));
DBMS_OUTPUT.PUT_LINE('Total Pay: ' || TO_CHAR(v_pay, '9G999G999D99'));
回答by Alex Poole
You're outputting a single string, so there is no opportunity for the values to be neatly aligned, as they could be in a simple select. The concatenation (||
) just sticks together exactly what it is given.
您正在输出单个字符串,因此没有机会将值整齐对齐,因为它们可能在一个简单的选择中。串联 ( ||
) 只是将所给的内容准确地粘在一起。
All you need to do is manually pad the strings:
您需要做的就是手动填充字符串:
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || (v_name));
DBMS_OUTPUT.PUT_LINE('Job: ' || (v_job));
DBMS_OUTPUT.PUT_LINE('Total Pay: ' || TRIM(TO_CHAR(v_pay, '9G999G999D99')));
The to_char
of the number column leaves it left-padded (right-aligned) to the width of the format mask:
该to_char
数列的叶子它左填充(右对齐)到格式掩模的宽度的:
SQL> select TO_CHAR(4.97,'9G999G999D99') from dual;
TO_CHAR(4.97,'
----------------
.97
... so in this case you want a trim
around that so it only uses your own padding.
... 所以在这种情况下,你想要一个trim
围绕它的,所以它只使用你自己的填充。
DECLARE
v_name varchar2(30) := 'Joe Bloggs';
v_job varchar2(20) := 'Contractor';
v_pay number := 52657.3;
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_name);
DBMS_OUTPUT.PUT_LINE('Job: ' || v_job);
DBMS_OUTPUT.PUT_LINE('Total Pay: '
|| TRIM(TO_CHAR(v_pay, '9G999G999D99')));
END;
/
Employee Name: Joe Bloggs
Job: Contractor
Total Pay: ,657.30
PL/SQL procedure successfully completed.
回答by Art
Display as columns - this is a simple way. You may employ Rpad(), Lpad() for this, which would be more advanced I guess:
显示为列 - 这是一种简单的方法。您可以为此使用 Rpad()、Lpad(),我猜这会更高级:
DECLARE
v_name varchar2(30) := 'Joe Bloggs';
v_job varchar2(20) := 'Contractor';
v_pay number := 52657.3;
BEGIN
DBMS_OUTPUT.PUT_LINE('Employee Name'||chr(9)||chr(9)||chr(9)||'Job'||chr(9)||chr(9)||chr(9)||chr(9)||chr(9)||'Total Pay');
DBMS_OUTPUT.PUT_LINE('--------------------------------------------------------');
DBMS_OUTPUT.PUT_LINE(v_name||chr(9)||chr(9)||chr(9)||chr(9)||chr(9)||v_job||chr(9)||chr(9)||TRIM(TO_CHAR(v_pay, '9G999G999D99')));
END;
/
Employee Name Job Total Pay
--------------------------------------------------------
Joe Bloggs Contractor ,657.30