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

解决方案 »

  1.   

    直接对可能是null的字段使用nvl(col1,0) 。
    这样如果col1的值是null,那么就替换成0
      

  2.   

    首先需要定位哪些列存在NULL,其次才是替换啊,最好是能给出具体的命令啊
      

  3.   

    table2 数据量不大时,直接在建表时加上约束条件或默认值即可。
      

  4.   

    楼主要考虑几个问题
    1、你的表结构是否可以修改,如果可以,就采用加默认值的方式
    2、如果表结构不能动,那么是否能够修改代码,如果能,就把不能为null的字段加上nvl函数。
    3、如果代码、表结构都不能动,那么你就写一条语句,将原表数据修改规范了。
      

  5.   

    执行如下脚本可查出有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;