Solved

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

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL select to return records that don't exist (crosjoin?) 3 40
Need more granular date groupings 4 44
rolling count by date, hour query 7 30
HIghlights of SSIS? 3 42
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

732 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