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
 Primary Key

Author  Topic 

Limuh
Yak Posting Veteran

94 Posts

Posted - 2008-05-05 : 03:03:38
Violation of PRIMARY KEY constraint 'PK_Dunning_TBL'. Cannot insert duplicate key in object 'Dunning_TBL'.
The statement has been terminated.

(0 row(s) affected)
Msg 2627, Level 14, State 1, Procedure GenerateFiles_FST_SP, Line 220
Violation of PRIMARY KEY constraint 'PK_Exceptions_TBL'. Cannot insert duplicate key in object 'Exceptions_TBL'.
The statement has been terminated.

i got this error how can i resolve this?

raky
Aged Yak Warrior

767 Posts

Posted - 2008-05-05 : 03:15:11
Hi,

You can get the number, but there's no way you can catch the error message
within t-sql code. You have to do this in the client application's error
handler

Be cool
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-05-05 : 03:19:45
Hi,

Well now you are trying to insert records into the parent tables which already exist.

Presumably your FK is there to prevent you adding records to the Child Table if there is no parent record. What you are doing here is trying to add records tot eh parent to make sure that the child record will have parents.

That's unusual - normally a Parent not existing would be treated as an error - rather than taking the "Create the parent just-in-time if necessary" approach.

In your first post add a test to check that the parents exist, and raise an error if they don't.

In your second post add an IF NOT EXISTS ... before each Insert of a parent record, so that they are only added if not already existing.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-05 : 04:24:07
Seems like you are trying to insert duplicate records with same PK value. You can avoid this by using:-
INSERT INTO Dunning_TBL (fields)
SELECT PK,....other fields
FROM YourSource y
LEFT JOIN Dunning_TBL d
ON d.PK=y.PK
WHERE d.PK IS NULL
Go to Top of Page

Limuh
Yak Posting Veteran

94 Posts

Posted - 2008-05-06 : 05:47:28
thank you very much : ) i always get the solution here hope you continue doing this.
Go to Top of Page
   

- Advertisement -