我需要在表的每一行中打印第一個非空列的值。我爲此使用了COALESCE。但是我想打印列的名稱和值。如何使用合併功能打印列名以及值
我的表看起來像這樣:
COL1 | COL2 | COL3 |COL4
********************************************************
|Value of col2| |Value of col4
---------------------------------------------------------
| |Value of col3|Value of col4
---------------------------------------------------------
Value of col1| | |Value of col4
---------------------------------------------------------
| | |
*********************************************************
我的代碼是這樣的:
declare
v_count integer;
v_counter integer;
cursor c is
select rowid, coalesce(col1,col2,col3,col4) not_null_value
from handson_table;
begin
select count(*) into v_count from handson_table;
dbms_output.put_line('There are '||v_count||' rows');
v_counter:=1;
for r in c
loop
dbms_output.put_line(rpad('*',5,'*')
|| 'Row Number' || v_counter
|| rpad('*',5,'*'));
if (length(r.not_null_value) > 1) then
dbms_output.put_line(r.not_null_value);
else
dbms_output.put_line('All columns of this row is NULL');
end if;
v_counter:=v_counter+1;
end loop;
end;
OUTPUT就這樣產生了:
There are 4 rows
*****Row Number 1*****
Value of col2
*****Row Number 2*****
Value of col3
*****Row Number 3*****
Value of col1
*****Row Number 4*****
All columns of this row is NULL
我要像輸出:
There are 4 rows
*****Row Number 1*****
col2 : Value of col2
*****Row Number 2*****
col3 : Value of col3
*****Row Number 3*****
col1 : Value of col1
*****Row Number 4*****
All columns of this row is NULL
請幫我一把。
真的找到你的解決方案很棒這樣做與案件確實照顧它。但我有一個問題,無論如何,這與coalesce做到這一點。我只是出於好奇而問,否則你的解決方案就是爆炸性的! –