-1

我有各种表格,每个表格都有超过一百个列,或者可以增加。此外,在我的表中,某些列的某些数据为空值,以及完全为空的列

我只需要具有完全空值的列或更好地理解该表的行数应该等于该表中存在的列的空数

DECLARE
    TYPE refc IS ref CURSOR;
    col_cv   REFC;
    l_query  VARCHAR(3999);
    v_rownum NUMBER;
    v_count  NUMBER;
BEGIN
    l_query := 'select rownum from &table_name ';

    FOR col IN (SELECT table_name,
                       column_name
                FROM   user_tab_columns
                WHERE  table_name = ' ') LOOP
        l_query := l_query
                   ||'DECODE('
                   ||col.column_name
                   ||',NULL,1,0)+';
    END LOOP;

    l_query := l_query
               ||'+0 as no_of_null_values from ... ';

    dbms_output.Put_line(l_query);

    OPEN col_cv FOR l_query;

    LOOP
        FETCH col_cv INTO v_rownum, v_count;

        EXIT WHEN col_cv%NOTFOUND;

        dbms_output.Put_line(v_rownum
                             || ' '
                             || v_count);
    END LOOP;

    CLOSE col_cv;
END; 
4

3 回答 3

1

像这样的东西:

select sum(case when column_1 is null then 1) as column_1_null_count,
       sum(case when column_2 is null then 1) as column_2_null_count,
       ... the above for all columns ...
       count(*) as total_rows
from the_table
where column_1_null_count = total_rows
   or column_2_null_count = total_rows

应该很容易生成基于动态的 user_tab_columns。

于 2013-09-05T10:46:17.597 回答
0

要计算列中的值,请使用 COUNT(column)。如果计数为 0,则该列中没有值,即该列在所有记录中为空(或表不包含任何记录)。这是一个查找所有不包含任何值的列的函数:

declare
  v_count integer;
begin
  for rec in (select table_name, column_name from user_tab_cols) loop
    execute immediate 'select count(' || rec.column_name || ') from ' || rec.table_name into v_count;
    if v_count = 0 then
      dbms_output.put_line(rec.table_name || '.' || rec.column_name);
    end if;
  end loop;
end;
于 2013-09-05T11:38:10.357 回答
0

我得到了答案:

set serveroutput on;
    declare
    CURSOR COLNAME IS SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE table_name='&table_name' ;
    tot number;
    stmt varchar2(500);
    begin
    FOR mrec IN COLNAME 
    LOOP
    --cname:= mrec.COLUMN_NAME;
    --tot := 0;
    stmt := 'select count(*)  from &table_name where ' || mrec.column_name  || ' is not null ' ;
     execute immediate stmt into tot;
    if tot = 0 then 
    dbms_output.put_line('COLUMN containing null IS ' || mrec.COLUMN_NAME);
    end if;
    end loop;
    end;
于 2013-09-06T05:01:46.127 回答