The Primary Key Violation is telling you that the at least one of the values for customer_key
in A.table
that you are trying to insert from B.Table
is already in use for a different Customer record in A
(and assuming that you've already run your delete statement for this customer_code
).
This means it is already too late to consider trying to keep the surrogate identity column customer_key
in synch between the two tables A and B (as you say you are not are in a position to truncate A
and copy across from scratch from B
, if applicable). However, it seems that customer_code
does not provide unique identification (idempotence) of a customer either (since the delete removed 10 rows).
So in summary - if you don't need to establish any link other than by customer_code
, and potentially via customer_name
, you can copy the data into A
which will be assigned new identity customer_key
's:
(i.e. leaving IDENTITY_INSERT
OFF)
insert into A.table(customer_code,custome_name)
select customer_code,customer_name
from B.table where customer_code='100'
Otherwise, if you do need to uniquely identify rows between the tables, what you will need to do is add new storage for the link between the 2 tables. A quick and dirty way would be to add B's surrogate directly to A, like so:
ALTER TABLE A.table ADD customer_key_TableB INT NULL -- Or whatever the type of `customer_key`
GO
Then insert and link the data like so (Again, with IDENTITY INSERT
for Table A still off):
insert into A.table(customer_code, customer_name, customer_key_TableB)
select customer_code, customer_name, customer_key
from B.table where customer_code='100'