1

我想要一个表中至少有一个非NULL数据条目的列的列表。

换句话说,我想获取以下至少返回一个条目的列名:

SELECT DISTINCT column_name FROM table WHERE column_name IS NOT NULL

我尝试了以下方法:

SELECT column_name
FROM information_schema.columns
WHERE table_name = "table_name"
AND EXISTS (
    SELECT DISTINCT column_name FROM table_name WHERE column_name IS NOT NULL
)

但这也会返回所有条目所在的列名NULL

那么如何只获取那些没有NULL条目的列呢?

4

2 回答 2

10

INFORMATION_SCHEMA.COLUMNS表中创建一个包含您希望执行的 SQL 的字符串,然后从该字符串准备一个语句并执行它。

我们希望构建的 SQL 如下所示:

  SELECT 'column_a'
  FROM   table_name
  WHERE `column_a` IS NOT NULL
  HAVING COUNT(*)
UNION ALL
  SELECT 'column_b'
  FROM   table_name
  WHERE `column_b` IS NOT NULL
  HAVING COUNT(*)
-- etc.

(可以省略WHERE子句并替换COUNT(*)COUNT(column),但我认为这在索引列上可能效率较低)。

这可以使用以下方法完成:

SET group_concat_max_len = 4294967295;

SELECT GROUP_CONCAT(
 ' SELECT ',QUOTE(COLUMN_NAME),
 ' FROM   table_name',
 ' WHERE `',REPLACE(COLUMN_NAME, '`', '``'),'` IS NOT NULL',
 ' HAVING COUNT(*)'
SEPARATOR ' UNION ALL ')
INTO   @sql
FROM   INFORMATION_SCHEMA.COLUMNS
WHERE  TABLE_SCHEMA = DATABASE()
   AND TABLE_NAME = 'table_name';

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

sqlfiddle上查看。

于 2012-11-15T17:26:19.110 回答
0

使用此过程,这将打印至少有一个非空行的表的列名。

create or replace procedure list_col_notNull(tblName in varchar2)
as
lv_col_name varchar2(200);
lv_ctr number;
lv_sql varchar2(400);
CURSOR cur_col_name is
SELECT column_name
FROM USER_TAB_COLUMNS U
WHERE table_name = tblName order by column_name asc;
begin
open cur_col_name;

LOOP
    FETCH cur_col_name INTO lv_col_name; 
    EXIT WHEN cur_col_name%NOTFOUND; 
    lv_sql := 'select count(1) From ' || tblName || ' where ' || lv_col_name || ' is not null'  ;  
    EXECUTE IMMEDIATE lv_sql into lv_ctr;
    if lv_ctr > 0
    then
        dbms_output.put_line(lv_col_name);
    end if;
于 2013-07-23T09:39:22.163 回答