现在有这样一个问题,有两个表因为要把其中一个表A里的数据导到另外一个表B里,但是A表里的唯一字段id和B表里的唯一字段ID有好多重复的,id共22位,如(3302030089200906289999),前面18位是固定的按照每天的日期和固定的10位代码,修改后面4位,且修改后的22位id不能和本表重复也不能和另外一个表里的数据重复.
求高手赐教

解决方案 »

  1.   

    后面四位有意义么?没有意义的话插入B表的时候把id修改下就可以了。
    如:max(substr(b.id,19,4))+substr(a.id,19,4)
      

  2.   

    A,B两个表如果没有对应ID的日期部分的日期字段,可以先分别加一个日期字段,然后再把A表的Id固定日期部分先减去一段时间,比如1年(注意不要和B表重复),再导入B表试试。另外用到日期,不要截取B表的ID的日期部分,直接用那个日期字段就可以了
      

  3.   

    这就是ID字段带有实际意义产生的后遗症,设计的时候就该把ID分为3个部分:
    ID、DATE、TAG,如:
    ID = 9999
    DATE = 20090628
    TAG = 3302030089
      

  4.   

    declare
    v_count number:=0;
    rd varchar2(4);
    begin
    for cur1 in (select * from a)loop
      select count(1) into v_count from b where id=cur1.id;
      if v_count=0 then 
        insert into b values(cur1.id,....);
      else
        loop
          rd:=to_char(round(dbms_random.value(0,9999)),'fm0000');
          select count(1) into v_count from b where id=substr(cur1.id,1,18)||rd;
          if v_count=0 then
            select count(1) into v_count from a where id=substr(cur1.id,1,18)||rd;
            if v_count=0 then
              insert into b values(substr(cur1.id,1,18)||rd,....);
              exit;
            end if;
          end if;
        end loop;
      end if;
    end loop;  
    end;   
      

  5.   

    分步使用临时表方式生成新ID,简单又清晰,供参考,效率如何还待楼主测试:
    1)建立A,B表字段ID的全量数据:
    create table id_a_b as select id from a union all select id from b;
    2)统计重复的ID:
    create table id_a_b_repeat as select id from id_a_b group by id having count(*) > 1;
    3)按前18位,取A,B表字段ID后4位的最大值:
    create table id_a_b_max as select substr(id,1,18) id_18,max(substr(id,19,4)) tail_max from id_a_b group by substr(id,1,18);
    4)取重复ID的新ID:
    create table id_a_b_repeat_new as select  id old_id, id_18||LPAD(tail_max+1,4,0) new_id from id_a_b_max a,id_a_b_repeat b
    where a.id like id_18||'%';
    5)id_a_b_repeat_new关联A表导入B表,实现方法很多。
      

  6.   


    忘记说了,大狭可以在补充点吗???现在我只想把重复的后面4位改掉,不重复不做处理,然后只把重复的插入B表里,插入B表以后A表的此条信息删除..
      

  7.   

    只把id有重复的插入B表,不重复的留在a表不作处理?
    declare
    v_count number:=0;
    rd varchar2(4);
    v_id varchar2(22);
    begin
    for cur1 in (select * from a for update)loop
      select count(1) into v_count from b where id=cur1.id;
      if v_count<>0 then 
     
        loop
          rd:=to_char(round(dbms_random.value(0,9999)),'fm0000');
          v_id:=substr(cur1.id,1,18)||rd;
          select count(1) into v_count from b where id=v_id;
          if v_count=0 then       
            select count(1) into v_count from a where id=v_id;
            if v_count=0 then
              insert into b values(v_id....);
              delete from a where id=cur1.id;
              exit;
            end if;
          end if;
        end loop;
      end if;
    end loop;  
    end; 
      

  8.   


    兄弟,我还一个没节的贴,和这一模一样,叫/? 高分求救高手解决数据从一个表插到另一个表 [Oracle][基础和管理] / 你去随便说两句我把分都给你.