Solved

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

Posted on 2010-08-12
5
472 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best counters for cpu high usage 3 33
insert wont work in SQL 14 22
SQL View nearest date 5 38
SQL Server syntax 4 23
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

832 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