oracle 如何格式化由 dbms_output.put_line PL/SQL 制作的表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17249225/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:44:50  来源:igfitidea点击:

How to format a table made by dbms_output.put_line PL/SQL?

oracleplsqlformat

提问by CHEBURASHKA

I have a procedure that produces a table in the spool:

我有一个程序可以在以下位置生成一个表spool

create procedure.....
......
loop
...
dbms_output.put_line(p_taskno||'   '||p_descrip||'  '||p_hrs||' '||p_start_date);
.........

The output is correct, however not well formatted:

输出正确,但格式不正确:

***************************************                                         
ABC Company         Projects:                                                   
Project: 5555                                                                   
TaskNo  Description      Hours StartDate                                        
_____________________________________________                                   
11      Prototype New Screens  30 23-AUG-06                                     
12      Convert Data Files  10.5 15-SEP-06                                      
13      Create Test Plan  15 14-AUG-06                                          
62      Develop Enterprise Model  280 26-OCT-06                                 
_____________________________________________                                   
Number of Tasks: 4                                                              
Total Project Hours: 335.5  

I cannot use column description format a20 word_wrappedbecause the table is the result of the dbms_output.put_line. How can I format it?

我无法使用,column description format a20 word_wrapped因为该表是dbms_output.put_line. 我该如何格式化?

回答by APC

You need to work out the maximum size of the column and then pad the output with spaces.

您需要计算出列的最大大小,然后用空格填充输出。

So you want tasknoto align with its header, that's six characters. Because it's a numeric you'll want it to align on the right:

所以你想taskno与它的标题对齐,这是六个字符。因为它是一个数字,你会希望它在右边对齐:

lpad(to_char(taskno), 6)

Whereas Description is a string, so you'll want to align it on the left

而 Description 是一个字符串,所以你需要在左边对齐它

rpad(p_description, 30)

Use these functions on the elements of the header too, to get the neatest output.

在标题的元素上也使用这些函数,以获得最整洁的输出。