473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 5729
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

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

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
1
by: Cliff | last post by:
I'm trying to do multiple insert statements. The table looks like this: CREATE TABLE $table (CNTY_CNTRY_CD char(3),ST char(2), CNTY_CNTRY_DESCR varchar(50),CNTY_CNTRY_IND char(1),...
2
by: John | last post by:
The ASP application inserts transaction records in transaction table with the system time as the primary key. However, it is possible to have primary key violation because the records in...
2
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers...
6
by: Nime | last post by:
Hi, I want to remove the row then add the fresh new one, so I don't have to decide to update or add. I always add, because I delete any existing row. However I'm stuck with the error below:...
2
by: embarkr | last post by:
I am getting the error: "Violation of PRIMARY KEY constraint 'PK_tblCustomsTariffTreeMap'. Cannot insert duplicate key in object 'dbo.tblCustomsTariffCodeTreeMap'." However, the record I am...
1
by: Zamdrist | last post by:
Violation of PRIMARY KEY constraint 'PK_CUSTOM2'. Cannot insert duplicate key in object 'MHGROUP.Custom2' Is there ANY other reason this violation of the primary key would happen OTHER than a...
7
by: akmaRudiliyn | last post by:
Hai everybody :). I have problem and need help. ERROR: ERROR Violation of PRIMARY KEY constraint 'PK_Table1_01'. Cannot insert duplicate key in object 'dbo.table1'. ERROR The statement has...
2
by: rorajoey | last post by:
Violation of UNIQUE KEY constraint 'IX_surveyQuestions'. Cannot insert duplicate key in object 'dbo.surveyQuestions'. This might seem like a simple matter of trying to insert a row with ID=20 when...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.