Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 getting Violation of PRIMARY KEY constraint

Author  Topic 

rob41
Yak Posting Veteran

67 Posts

Posted - 2010-09-30 : 12:06:43
I getting violation of primary key constraint because i'm trying to merge two tables that have the same records. Table 1 has old records only. Table 2 has old and new records. I only want to get new records from merge while keeping old records which are locked. I tried adding a where statement at the end where Table 1 [Id] <> Table 2 [Id] this gives me the message The multi-part identifier "Table 1. Id" could not be bound.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-30 : 12:08:56
it should be

INSERT INTO Table1
SELECT columns
FROM Table2 t
WHERE NOT EXISTS (SELECT 1 FROM Table1 WHERE PK=t.PK)


PK is primray key of the tables

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rob41
Yak Posting Veteran

67 Posts

Posted - 2010-10-01 : 15:54:27
that worked thanks :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-02 : 02:48:40
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -