Solved

Primary key constraint violation problem using Insert in SQL Server Management Studio 2008

Posted on 2010-08-12
5
477 Views
Last Modified: 2012-05-10
I am getting the following error when trying to insert new records into a table.
Msg 2627, Level 14, State 1, Line 7
Violation of PRIMARY KEY constraint 'MLFML_PK'. Cannot insert duplicate key in object 'dbo.MLFML'.
The statement has been terminated.

Here is my code:

declare @TODAY varchar(10)
SET @TODAY = (SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY])
 
 
INSERT INTO MLFML (BOOK_QTY, CREATED_BY, DATE_CREATED, DATE_LAST_ACT, DATE_LAST_ISSUE, DATE_LAST_PHY, DATE_LAST_PRINT,
DATE_LAST_RECPT, MLFML_USER_1,  MLFML_USER_2, MLFML_USER_3, MLFML_USER_4, ON_HAND_QTY, OPERATOR_ID, PART_ID, PHYSICAL_QTY, 
STOCK_LOCATION, STORES_CODE, DATE_LAST_UPDT,  TIME_LAST_UPDT,  DELETE_FLAG) 

SELECT 0, 'SYS.DM', GETDATE(), @TODAY, NULL, @TODAY, NULL, @TODAY, 0, 0, '', '', PI.QUANTITY, 'SYS.DM', PI.PART_ID, 0, PI.TO_LOCATION, 
PI.STORES_CODE,  convert(char(11),getdate(),101), convert(char(5),getdate(),108), 'N'

FROM PISTOCKTRANSFER PI, MLFML ML
WHERE ((PI.STORES_CODE <> ML.STORES_CODE) AND (PI.PART_ID <> ML.PART_ID) AND (PI.TO_LOCATION <> ML.STOCK_LOCATION))

Open in new window


The primary key is STORES_CODE, PART_ID, STOCK_LOCATION

I try and validate the records by using the following select:

 
SELECT * FROM MLFML ML
JOIN PISTOCKTRANSFER PI
ON PI.PART_ID = ML.PART_ID
WHERE (ML.STORES_CODE = PI.STORES_CODE) AND (ML.STOCK_LOCATION = PI.TO_LOCATION)

Open in new window


Of course I get 0 results from the above SELECT statement, however I get the primary key constraint when trying to insert.  
Please help
0
Comment
Question by:rbmacct
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:Cboudroz
ID: 33422415
you need to used left join or if not exists

 
-- whit a left join
SELECT 
	* 
FROM 
	PISTOCKTRANSFER PI
	left join MLFML ML	
		ON PI.PART_ID = ML.PART_ID
			and PI.STORES_CODE = ML.STORES_CODE
			and PI.STOCK_LOCATION = ML.STOCK_LOCATION
WHERE 
	ml.PART_ID is null


-- whit if not exists
SELECT 
	* 
FROM 
	PISTOCKTRANSFER PI
WHERE 
	not exists 
	(
			select 
				*
			from 
				MLFML ML	
			where 
				PI.PART_ID = ML.PART_ID
				and PI.STORES_CODE = ML.STORES_CODE
				and PI.STOCK_LOCATION = ML.STOCK_LOCATION
	)

Open in new window

0
 
LVL 1

Author Comment

by:rbmacct
ID: 33422910
The problem is with the Insert, not the Select.

However, for information only, I did use the exact where clause with the "not exists" in my insert statement and still got the same error message.
0
 
LVL 7

Expert Comment

by:Cboudroz
ID: 33423866
--The problem is with the Insert, not the Select.
I know the select I sent was to put in the insert statement.

--However, for information only, I did use the exact where clause with the "not exists" in my insert statement and still got the same error message.

2 possibility,
1- your primary key is not STORES_CODE, PART_ID, STOCK_LOCATION
2- you have a trigger on the table MLFML  trying to insert something that create duplicate

 
declare @TODAY varchar(10)
SET @TODAY = (SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY])
 
 
INSERT INTO MLFML (BOOK_QTY, CREATED_BY, DATE_CREATED, DATE_LAST_ACT, DATE_LAST_ISSUE, DATE_LAST_PHY, DATE_LAST_PRINT,
DATE_LAST_RECPT, MLFML_USER_1,  MLFML_USER_2, MLFML_USER_3, MLFML_USER_4, ON_HAND_QTY, OPERATOR_ID, PART_ID, PHYSICAL_QTY, 
STOCK_LOCATION, STORES_CODE, DATE_LAST_UPDT,  TIME_LAST_UPDT,  DELETE_FLAG) 
-- whit if not exists
SELECT 
	* 
FROM 
	PISTOCKTRANSFER PI
WHERE 
	not exists 
	(
			select 
				*
			from 
				MLFML ML	
			where 
				PI.PART_ID = ML.PART_ID
				and PI.STORES_CODE = ML.STORES_CODE
				and PI.STOCK_LOCATION = ML.STOCK_LOCATION
	)

Open in new window

0
 
LVL 1

Accepted Solution

by:
rbmacct earned 0 total points
ID: 33424079
Solution found.  Solution is as follows:

INSERT INTO MLFML (BOOK_QTY, CREATED_BY, DATE_CREATED, DATE_LAST_ACT, DATE_LAST_ISSUE, DATE_LAST_PHY, DATE_LAST_PRINT, DATE_LAST_RECPT, MLFML_USER_1,
MLFML_USER_2, MLFML_USER_3, MLFML_USER_4, ON_HAND_QTY, OPERATOR_ID, PART_ID, PHYSICAL_QTY, STOCK_LOCATION, STORES_CODE, DATE_LAST_UPDT,
TIME_LAST_UPDT,  DELETE_FLAG)

SELECT 0, 'SYS.DM', GETDATE(), @TODAY, NULL, @TODAY, NULL, @TODAY, 0, 0, '', '', PI.QUANTITY, 'SYS.DM', PI.PART_ID, 0, PI.TO_LOCATION, PI.STORES_CODE,
convert(char(11),getdate(),101), convert(char(5),getdate(),108), 'N'

FROM PISTOCKTRANSFER PI
WHERE NOT EXISTS (SELECT 1 FROM MLFML WHERE (STORES_CODE = PI.STORES_CODE) AND (PART_ID = PI.PART_ID) AND (STOCK_LOCATION = PI.TO_LOCATION)




0
 
LVL 1

Author Comment

by:rbmacct
ID: 33424239
Found solution elsewhere.  Updating posting with a solution that works.  Tried to award some points to Cboudroz but the system did not let me.  It was all or nothing.  It would not let me award partial points without awarding all the points.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question