导入数据,判断与主键重复数据,重复的数据就更新原来的数据,不重复的数据就直接导入!

解决方案 »

  1.   

    先查重复的,更新,再将不重复的插入
    update ....
    insert into ........
      

  2.   

    两个数据库t1,t2,一个adoquery1,将t1的表b1中的数据导入t2的表b2中,主键t_id,导入数据,判断与主键重复数据,重复的数据就更新原来的数据,不重复的数据就直接导入!,最简单的sql语句!
      

  3.   

    merge into table1 using table2 on(join_condition) when matched update set col1=value
    when not matched insert (colum_list) values(colum_values)
    用这个就ok了,自己试试吧
      

  4.   

    这个语句很爽的哦,是偶看oracle全英文考试题库看来的,很辛苦哦,与你分享吧!!!
      

  5.   

    分两步做,先更新表的相同记录,再插入不同的记录。update 数据库t2.dbo.表 
    set 数据库t2.dbo.表.字段 = 数据库t1.dbo.表.字段 
    from 数据库t2.dbo.表 
    where 数据库t1.dbo.表.id=数据库t2.dbo.表.idinsert into 数据库t2.dbo.表 
    select * from 数据库t1.dbo.表 where 数据库t1.dbo.表.id not in (select id from 数据库t2.dbo.表)
      

  6.   

    adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('select * from b where sfzh1 in (select sfzh1 from b group by sfzh1 having count(*)>1)');
      adoquery1.Open;//选择重复记录  adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('select * from b ');
      adoquery1.Open;//选择所有记录  adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('delete from b where sfzh1 in (select sfzh1 from b group by sfzh1 having count(*)>1)');
      adoquery1.ExecSQL;
      adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('select * from b ');
      adoquery1.Open;//删除所有重复记录  adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('alter table b add idMytest int identity(1,1)');
      adoquery1.ExecSQL;  adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('delete from b where sfzh1 in (select sfzh1 from b where b.idMytest<idMytest '+
                        'group by sfzh1 having count(*)>1)');
      adoquery1.ExecSQL;  adoquery1.Close;
      adoquery1.SQL.Clear;
      adoquery1.SQL.Add('alter table b drop column idMytest');
      adoquery1.ExecSQL;//保留一条记录
      

  7.   

    procedure TForm1.Button9Click(Sender: TObject);
    var
      AdoquerySource,Adoquery : Tadoquery;  // Adoquerymd,
    begin
      try
        AdoquerySource.Connection:=ADOConnection2;
    //    Adoquerymd.Connection:=ADOConnection3;
        Adoquery.Connection:=ADOConnection3;    AdoquerySource:=TAdoquery.Create(self);
    //    Adoquerymd:=TAdoquery.Create(self);
        Adoquery:=TAdoquery.Create(self);
        AdoquerySource.SQL.Clear;
        AdoquerySource.SQL.Add('Select * from b');
        AdoquerySource.Open;
        while AdoquerySource.Eof do
        begin
          Adoquery.Close;
          Adoquery.SQL.Clear;
          Adoquery.SQL.Add('Select * from xxsqxmk where sfzh1=:sfzh1');
          Adoquery.Parameters.ParamByName('sfzh1').Value:=AdoquerySource.fieldbyname('sfzh1').Value;
          Adoquery.Open;
          if Adoquery.RecordCount>0 then
          begin
            showmessage('已经存在')
          end else
          begin
            //dosomething
          end;
        end;
      finally
        freeandnil(AdoquerySource);
        freeandnil(Adoquery);
      end;
    end;
      

  8.   

    if exists (select * from tablename where field1 = :field)
     update tablename set ...
    else
     insert tablename ...
      

  9.   

    merge into table1 using table2 on(join_condition) when matched then update set col1=value
    when not matched then insert (colum_list) values(colum_values)一句话就解决了,为什么还要那么多代码,那么复杂的过程来实现呢
      

  10.   

    楼上,
    关键是你写的SQL只能用于oracle,不通用阿。
      

  11.   

    樓上的zdyrain(雨) ,你有沒有在sqlserver 2000下做兩個數據庫做做看阿,檢驗一下你的sql語句是否正確呢,我視了一下,好像部可以的阿,建議你試正確了再貼上來,
      

  12.   

    merge into table1 别名1 
    using table2 别名2
    on(join_condition) 
    when matched then 
    update set col1=value
    when not matched then 
    insert (colum_list) values(colum_values)这个在oracle下是可以的,sql server2000下好些不行
      

  13.   

    先用 having count(*)>1 查出重复的. 然后再更新为自己想要的数据
      

  14.   

    分两步做,先更新表的相同记录,再插入不同的记录。update t2
    set t2.b2= t1.b1
    from t2,t1
    where t1.t_id=t2.t_idinsert into t2
    select * from t1 where t1.t_id not in (select t_id from t2)
      

  15.   

    两个数据库t1,t2,一个adoquery1,将t1的表b1中的数据导入t2的表b2中,主键t_id
      

  16.   

    用dataTable 存储过程+游标 触发器 创建临时表