AB两个数据库间需要同步复制表A。两个表的数据结构一样。fid1+fid2组成联合主键。
在当前数据库的查询分析器执行:
insert into dbo.Int_Sales_Orders
select * from ert.Intface_4_FS_KD.dbo.Int_Sales_Orders a
where a.FOrder_Number+a.Forder_Line_Number not in 
  (select FOrder_Number+Forder_Line_Number from dbo.Int_Sales_Despatch)
提示:“违反了 PRIMARY KEY 约束 'PK_Int_Sales_Orders'。不能在对象 'Int_Sales_Orders' 中插入重复键。
语句已终止。”
请教解决方法。

解决方案 »

  1.   

    你应该这样,下面是一个例子,COL1,COL2是联合主键:use tempdb
    gocreate table t1(col1 int,col2 char(1))
    create table t2(col1 int,col2 char(1))
    goinsert t1 select 1,'a'
    union all select 1,'b'
    goinsert t2 select 1,'b'
    union all select 1,'c'
    union all select 2,'d'
    go
    select * from t1
    select * from t2
    goinsert t2(col1,col2)
    select
    a.col1,
    a.col2
    from t1 a
    where not exists(
    select 

    from t2
    where col1 = a.col1
    and col2 = a.col2)select * from t1
    select * from t2
    go
    drop table t1
    drop table t2
    go你的代码是想当然的写法,理会一下这个例子.