oracle中如何查询一条记录中都有那个字段是空值

oracle中如何查询一条记录中都有那个字段是空值

需要用到循环及动态sql。

如test表中有如下数据,其中id和name列有空值。

执行以下内容:

declare 
v_count int;--定义变量
v_col varchar2(20);--定义变量
v_sql varchar2(2000);--定义变量
v_last_col varchar2(20);--定义变量
cursor cur_col is select column_name from user_tab_cols where table_name='TEST' order by column_id;--定义游标
begin
  select column_name into v_last_col  from user_tab_cols where table_name='TEST' and column_id=(select max(column_id) from user_tab_cols where table_name='TEST');--取出表中最后一个字段放入v_last_col
  open cur_col;--打开游标
         loop --执行循环
           fetch cur_col into v_col;--取出游标内容到变量v_col
           v_sql:='select count(*) from TEST where '||v_col||' is null';
           execute immediate v_sql into v_count;--执行动态sql
           if v_count>0--如果查询有空值的字段不为空,则输出此字段名
             then
           dbms_output.put_line(v_col); 
           end if;
           exit when v_col=v_last_col; --退出循环条件
         end loop;--结束循环
      close cur_col;--关闭游标
end;

执行结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-08-21
可以用这样的方法 :把每个字段count一下,再与count(1)比较下;
select count(1),count(字段1),count(字段2),···count(字段n)
from table1;
结果小于count(1)的字段就有空值。本回答被提问者采纳
第2个回答  2012-02-25
KC8段?
相似回答