SQL*Plus 中的条件列格式
Conditional column format in SQL*Plus
我需要根据返回值的长度和列名的长度(在我的例子中是 "heads_results")在 SQL*Plus 中格式化一个 repot。
select 语句的结果:
head_results
**********************************************************
value_1
value_11
value_222222222
value_99999999999999999999999999999999999999999999999999999999999999
我需要根据任何行返回的最大长度值来格式化列 "head_results" 的长度(在本例中为 length('value_99999999999999999999999999999999999999999999999999999999999999')
)。
如果没有返回值或返回的最大长度值小于 length ('head_results')
,则将 column_name 的长度格式化为其长度。
SQL*Plus 可以吗?
您可以使用替换变量和 SQL*Plus column ... new_value ...
语法从查询中定义其值:
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0), length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
查询:
- 用
max(length(head_results))
找到table中最长的值;
- 如果
nvl(..., 0)
只有空值(或没有数据),则默认为零;
- 使用
greatest(..., length('head_results'))
找到该值和固定字符串中的较大者,但如果您愿意,可以使用固定值 12;
- 并为该表达式的结果提供别名
col_width
.
然后 column col_width new_value col_width noprint
允许您使用 col_width
作为替换变量,它从查询中继承值。
然后 column head_results format "a&col_width"
将列宽设置为查询返回的字符数,使用该替换变量 - a&col_width
转换为 a12
,或 a15
,或a68
,或其他。
当您进行实际查询时,该列将以该宽度显示。
Demo with a dummy table,最初是一个短值,标题宽度为 12 个字符:
create table your_table (head_results varchar2(80));
insert into your_table (head_results)
values ('value_1');
1 row inserted.
set termout off
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0),
length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
set termout on
select head_results from your_table;
HEAD_RESULTS
------------
value_1
添加的值越长,它就会变得越宽:
insert into your_table (head_results)
values ('value_222222222');
1 row inserted.
set termout off
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0), length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
set termout on
select head_results from your_table;
HEAD_RESULTS
---------------
value_1
value_222222222
并且你的最长值仍然足够宽:
insert into your_table (head_results)
values ('value_99999999999999999999999999999999999999999999999999999999999999');
1 row inserted.
set termout off
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0), length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
set termout on
select head_results from your_table;
HEAD_RESULTS
--------------------------------------------------------------------
value_1
value_222222222
value_99999999999999999999999999999999999999999999999999999999999999
我需要根据返回值的长度和列名的长度(在我的例子中是 "heads_results")在 SQL*Plus 中格式化一个 repot。
select 语句的结果:
head_results
**********************************************************
value_1
value_11
value_222222222
value_99999999999999999999999999999999999999999999999999999999999999
我需要根据任何行返回的最大长度值来格式化列 "head_results" 的长度(在本例中为 length('value_99999999999999999999999999999999999999999999999999999999999999')
)。
如果没有返回值或返回的最大长度值小于 length ('head_results')
,则将 column_name 的长度格式化为其长度。
SQL*Plus 可以吗?
您可以使用替换变量和 SQL*Plus column ... new_value ...
语法从查询中定义其值:
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0), length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
查询:
- 用
max(length(head_results))
找到table中最长的值; - 如果
nvl(..., 0)
只有空值(或没有数据),则默认为零; - 使用
greatest(..., length('head_results'))
找到该值和固定字符串中的较大者,但如果您愿意,可以使用固定值 12; - 并为该表达式的结果提供别名
col_width
.
然后 column col_width new_value col_width noprint
允许您使用 col_width
作为替换变量,它从查询中继承值。
然后 column head_results format "a&col_width"
将列宽设置为查询返回的字符数,使用该替换变量 - a&col_width
转换为 a12
,或 a15
,或a68
,或其他。
当您进行实际查询时,该列将以该宽度显示。
Demo with a dummy table,最初是一个短值,标题宽度为 12 个字符:
create table your_table (head_results varchar2(80));
insert into your_table (head_results)
values ('value_1');
1 row inserted.
set termout off
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0),
length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
set termout on
select head_results from your_table;
HEAD_RESULTS
------------
value_1
添加的值越长,它就会变得越宽:
insert into your_table (head_results)
values ('value_222222222');
1 row inserted.
set termout off
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0), length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
set termout on
select head_results from your_table;
HEAD_RESULTS
---------------
value_1
value_222222222
并且你的最长值仍然足够宽:
insert into your_table (head_results)
values ('value_99999999999999999999999999999999999999999999999999999999999999');
1 row inserted.
set termout off
column col_width new_value col_width noprint
select greatest(nvl(max(length(head_results)), 0), length('head_results')) as col_width
from your_table;
column head_results format "a&col_width"
set termout on
select head_results from your_table;
HEAD_RESULTS
--------------------------------------------------------------------
value_1
value_222222222
value_99999999999999999999999999999999999999999999999999999999999999