日期:2014-05-17  浏览次数:20808 次

如何定位表中NULL的列名
我现在需要将一个数据库中的数据移到另一个数据库中,其中每张表的方法如下:
INSERT INTO table2 SELECT * FROM table1 ;
但是table1可能存在一些NULL的字段,所以我需要UPDATE这些字段为空格或者0,请问如何才能快速定位出这些NULL的列名啊?


------解决方案--------------------
直接对可能是null的字段使用nvl(col1,0) 。
这样如果col1的值是null,那么就替换成0
------解决方案--------------------
table2 数据量不大时,直接在建表时加上约束条件或默认值即可。
------解决方案--------------------
找数据字典里?
------解决方案--------------------
执行如下脚本可查出有null值的列:
declare
i_l_count pls_integer;
begin
for c in (select * from user_tab_cols t where t.table_name not like 'BIN$%') loop
execute immediate 'select count(*) from ' || c.table_name || ' where ' || c.column_name || ' is null'
into i_l_count;

if i_l_count > 0 then
dbms_output.put_line('select ' || c.column_name || ' from ' || c.table_name || ' where ' || c.column_name || ' is null;');
end if;
end loop;

dbms_output.put_line('ok');
exception
when others then
dbms_output.put_line(sqlerrm);
end;