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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Identity

Author  Topic 

sardinka
Posting Yak Master

142 Posts

Posted - 2005-05-26 : 14:34:43
I am getting this error because my contact_id is a PK and
has the identity set to yes and Identity seed=1,Idenity increment=1
'Server: Msg 2627, Level 14, State 1, Line 4
Violation of PRIMARY KEY constraint 'PK__CONTACT__40C49C62'. Cannot insert duplicate key in object 'CONTACT'.
The statement has been terminated.'
I am trying to insert a new rows with some old data. Below is my query.
What should I do in order to accoplish my task?


set IDENTITY_INSERT contact ON
insert into contact (contact_id, contact_type_id, account_id,
name, phone_number, extension, fax_number,
email_address, date_created, created_by,
last_update_date, last_updated_by)

select (contact_id, ,contact_type_id, 1003,
name, phone_number, extension, fax_number,
email_address, getdate(), created_by,
getdate(), last_updated_by
from contact
where aid = 916
set IDENTITY_INSERT contact OFF

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-05-26 : 15:01:10
You want to intentionally add dupes? Why?
You obviously can't use the the same PK value more than once. If you want to add all the data except the primary key (which I assume is the identity column), just remove the identity column from both the insert colList and the select colList. And remove the "set IDENTITY_INSERT ON/OFF" statements. That will give you the same old data with new generated ID values.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -