sql - 如何使用合并打印列名称和值

标签 sql oracle plsql

我需要打印表的每一行中第一个非空列的值。我为此使用 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;

输出如下:

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

请帮帮我。

最佳答案

这是你想要的吗:

select (case when col1 is not null then 'col1 : ' || col1
             when col2 is not null then 'col2 : ' || col2
             when col3 is not null then 'col3 : ' || col3
             when col4 is not null then 'col4 : ' || col4
             else 'All Null'
        end)
from handson_table;

编辑:

是的,您可以使用COALESCE() 来完成此操作,但这种简单的方法在 Oracle 中不起作用。所以,这并不能达到你想要的效果:

select COALESCE('col1 : ' || col1,
                'col2 : ' || col2,
                'col3 : ' || col3,
                'col4 : ' || col4,
                'All Null')
from handson_table;

问题在于 Oracle 将 NULL 视为连接的空字符串。所以,你最终会得到类似的东西

如果您可以使用单个查询,则不需要 PL/SQL 代码块。

select COALESCE(NULLIF('col1 : ' || col1, 'col1: '),
                NULLIF('col2 : ' || col2, 'col2: '),
                NULLIF('col3 : ' || col3, 'col3: '),
                NULLIF('col4 : ' || col4, 'col4: '),
                'All Null')
from handson_table;

关于sql - 如何使用合并打印列名称和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32073789/

相关文章:

java - 通过 Oracle SQL Developer 工具加载 Java 文件

sql - 在不指定架构名称的情况下访问表

oracle - 在 oracle 中批量收集

sql - 令人困惑的 SELECT 语句

.net - 哪个更快 : Working from memory or lots of queries?

php - Prestashop Tools::getValue() 函数不逃避 sql 注入(inject)?

oracle - 如何在dbms_sql.open_cursor上解决ORA-29471?

c# - Linq to SQL 检查数据是否存在并更新/插入,具体取决于

sql - 在 PL/SQL 中,如何根据下一行更新一行?

database - Oracle 执行计划改变