470,594 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Violation of PRIMARY KEY constraint 'PK_Insured_Person'. Cannot insert duplicate key

I have to Insert records through select record from a table with group by clause within a store procedure. but when i am executing the procedure, It is showing error" Violation of PRIMARY KEY constraint 'PK_Insured_Person'. Cannot insert duplicate key in object 'dbo.Insured_Person'."
Code is like that:

Insert Into Table
(
Id,
Columns
)
Select
Min(Id) as Id,
Columns

Where
column=data
order by
columns

How can i find out the solution and where i need to check.


Please suggest me.

Thanks

Ashutosh Jha
Sep 13 '07 #1
3 5622
davef
98
Set identity property to ON on the Primary Key field. Then, You won't have to worry about calculating the keys at all.

CREATE TABLE [dbo].[tblTest] (
[nID] [int] IDENTITY (1, 1) NOT NULL ,
[szDesc] [varchar] (50) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblTest] WITH NOCHECK ADD
CONSTRAINT [PK_tblTest] PRIMARY KEY CLUSTERED
(
[nID]
) ON [PRIMARY]
GO

INSERT INTO [dbo].[tblTest] (szDesc)
VALUES ('New Description')

INSERT INTO [dbo].[tblTest] (szDesc)
VALUES ('Another Description')
Sep 13 '07 #2
ck9663
2,878 Expert 2GB
I have to Insert records through select record from a table with group by clause within a store procedure. but when i am executing the procedure, It is showing error" Violation of PRIMARY KEY constraint 'PK_Insured_Person'. Cannot insert duplicate key in object 'dbo.Insured_Person'."
Code is like that:

Insert Into Table
(
Id,
Columns
)
Select
Min(Id) as Id,
Columns

Where
column=data
order by
columns

How can i find out the solution and where i need to check.


Please suggest me.

Thanks

Ashutosh Jha

the table you are inserting to has a PRIMARY KEY CONSTRAINT. constraint are usually put in place to prevent (among others) inserting duplicate records. try analyzing the table you are getting the data from. maybe you have to check those duplicate first before you proceed with the insert.
Sep 13 '07 #3
amitpatel66
2,367 Expert 2GB
I have to Insert records through select record from a table with group by clause within a store procedure. but when i am executing the procedure, It is showing error" Violation of PRIMARY KEY constraint 'PK_Insured_Person'. Cannot insert duplicate key in object 'dbo.Insured_Person'."
Code is like that:

Insert Into Table
(
Id,
Columns
)
Select
Min(Id) as Id,
Columns

Where
column=data
order by
columns

How can i find out the solution and where i need to check.


Please suggest me.

Thanks

Ashutosh Jha
Run below Query that will help u find out the ID that is already in Table:

Expand|Select|Wrap|Line Numbers
  1. SELECT id FROM tables WHERE id IN (Select Min(Id) from columns Where column=data order by columns)
  2.  
Sep 14 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by John Simmons | last post: by
6 posts views Thread by Nime | last post: by
1 post views Thread by Zamdrist | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.