--通过主键来判断是否已经有? 假设是:--更新已经存在的
update table1 set 字段1=b.字段1,字段2=b.字段2,...字段n=b.字段n
from table1 a join 结果集 b on a.主键=b.主键--插入不存在的
insert table1
from table1 a lef join 结果集 b on a.主键=b.主键
where b.主键 is null

解决方案 »

  1.   

    --或写成触发器来控制,这样你只管插入数据就行了.create trigger tr_insert on table1
    instead of insert
    as
    --更新已经存在的
    update table1 set 字段1=b.字段1,字段2=b.字段2,...字段n=b.字段n
    from table1 a join inserted b on a.主键=b.主键--插入不存在的
    insert table1
    from table1 a lef join inserted b on a.主键=b.主键
    where b.主键 is null
    go
      

  2.   

    建议你用ADODB.RECORDSET的相关功能做吧
      

  3.   

    下面适用于主键时多个字段组合键的情况:update Table1
    set f1 = ...
       ,f2 = ...
       ...
       ,fn = ...
    from Table11
         where Table1.主键1 = Table11.主键1
               and  Table1.主键2 = Table11.主键2
     insert into table1
    select *
    from table11
    where not exists (select * 
                       from table1 
                      where 主键1 = Table11.主键1 
                             and  主键2 = Table11.主键2 )
      

  4.   

    --插入不存在的
    insert table1
    from table1 a lef join 结果集 b on a.主键=b.主键
    where b.主键 is null方法还真多。
    老邹的第一个方法的插入,如果换成下面的语句是不是更好理解-------------------------------------------
    insert into a 
    select * from b where 主键 not in(select 主键 from a)
      

  5.   

    谢谢各位指点。已通过。
    问题仍然在上例中。
    set A1=b.A1, A2=b.A2, A3=b.A3 An....
    A.A1, A.A2,A.A3...等等都是一些统计值。
    如果要求A.A1, A.A2,A.A3的值>0才修改,=0则不做任何改动,能做到??
      

  6.   

    --更新已经存在的
    update table1 set 字段1=b.字段1,字段2=b.字段2,...字段n=b.字段n
    from table1 a join 结果集 b on a.主键=b.主键
    where a.a1>0 and a.a2>0 and a.a3>0  -------****加上条件--插入不存在的
    insert table1
    from table1 a lef join 结果集 b on a.主键=b.主键
    where b.主键 is null
      

  7.   

    如果要求A.A1, A.A2,A.A3的值>0才修改,=0则不做任何改动,能做到??
    完了完了,应该是‘如果要求A.A1, A.A2,A.A3的值分别>0才修改,=0则不做任何改动,能做到??”怪没有说清楚,我想应该是or。先试试,不行再问。先给分。
      

  8.   

    假設結果集為#tempdelete Table1
    where id in (select  id  from Table1
    where id not in (select id from #temp) )
    insert into Table1
    select * from #temp