2

我正在使用 oracle 数据库,我基本上需要做的是计算某个表中每列的 NULL 字段数。

类似的东西:

DECLARE
    BlankCount number(20);
    i number(2) := 1;

BEGIN
    loop that would take each column individualy and exit after the last one
        SELECT COUNT(*) INTO BlankCount FROM name_of_my_table
        DBMS_OUTPUT.PUT_LINE('Column '||i||' has '||BlankCount||' empty cells');
        i := i + 1;
    END LOOP;
END;

我只是找不到任何可以做循环部分的东西。如果我可以显示列名而不是仅对它们进行编号(使用 i),那也很好(但这不是很重要)。

谢谢!

4

2 回答 2

5

像这样的东西:

declare 

  mytable varchar(32) := 'MY_TABLE';

  cursor s1 (mytable varchar2) is 
            select column_name 
            from user_tab_columns
            where table_name = mytable
            and nullable = 'Y';

  mycolumn varchar2(32);
  query_str varchar2(100);    
  mycount number;

begin

  open s1 (mytable);

  loop
     fetch s1 into mycolumn; 
         exit when s1%NOTFOUND;

     query_str := 'select count(*) from ' || mytable || ' where '  || mycolumn || ' is null';

     execute immediate query_str into mycount;

     dbms_output.put_line('Column ' || mycolumn || ' has ' || mycount || ' null values');

  end loop;
end; 
于 2012-07-25T03:37:26.620 回答
0

Try using cursor approach and Dynamic SQL as mentioned in this thread: How to loop through columns in an oracle pl/sql cursor

HTH.

于 2012-07-25T03:35:35.350 回答