SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 INSERT STATEMENT FAILING WITH MULTIPLE VALUES
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Chris_Kelley
Posting Yak Master

102 Posts

Posted - 08/19/2014 :  12:19:17  Show Profile  Reply with Quote
Hi All,

if I run --

BEGIN TRAN
INSERT INTO SDMAST -- (SD_KEY,SD_TYPE,EDIT_SECURITY,MULTI_RECORDS)
VALUES ((SELECT MAX(SD_KEY)FROM SDMAST)+1,'CLIENT_NAME','C',0,0)

the table is updated perfectly, but if I run --

BEGIN TRAN
INSERT INTO SDMAST -- (SD_KEY,SD_TYPE,EDIT_SECURITY,MULTI_RECORDS)
VALUES ((SELECT MAX(SD_KEY)FROM SDMAST)+1,'CLIENT_NAME','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'PORT_ID','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'DOA1','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'STATUS_DATE','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'CUST_TYP','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'LOAN_CD_TYP','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'BAL','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'DATE_LPAY','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'AMT_LPAY','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'CHRG_OFF_D','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'CHRG_OFF_A','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'SETT_AUTH','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'NAME','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_SUFFIX','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'PATIENT_NM','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_EMP_STREET','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_EMP_STREET2','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_EMP_CITY','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_EMP_STATE','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_EMP_ZIP','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'ORIG_CRED','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'FRST_DT_DELQ','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'AGENCY_COMM','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'PALL_ACCTNO','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'ORIG_ACCTNO','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_PHYSICIAN','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'D_EMP','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'FACILITY_NM','C',0,0),
((SELECT MAX(SD_KEY)FROM SDMAST)+1,'DIAG_CD','C',0,0)

I get --

Msg 2601, Level 14, State 1, Line 2
Cannot insert duplicate key row in object 'dbo.SDMAST' with unique index 'PK_SDMAST'.
The statement has been terminated.



Can someone help me through this?


Thanks,
Chris
Jr Programmer

tkizer
Almighty SQL Goddess

USA
37129 Posts

Posted - 08/19/2014 :  12:43:20  Show Profile  Visit tkizer's Homepage  Reply with Quote
You are trying to add the same MAX+1 value multiple times as it's only computed once for your one insert. If you need different values, then use insert/select multiple times.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Ifor
Aged Yak Warrior

590 Posts

Posted - 08/19/2014 :  12:43:55  Show Profile  Reply with Quote
You may want to re-think your design.

WITH NewRows
AS
(
	SELECT *
	FROM
	(
		VALUES (1,'CLIENT_NAME','C',0,0)
		,(2,'PORT_ID','C',0,0)
		,(3,'DOA1','C',0,0)
		,(4,'STATUS_DATE','C',0,0)
		,(5,'CUST_TYP','C',0,0) 
		,(6,'LOAN_CD_TYP','C',0,0)
		,(7,'BAL','C',0,0)
		,(8,'DATE_LPAY','C',0,0)
		,(9,'AMT_LPAY','C',0,0)
		,(10,'CHRG_OFF_D','C',0,0) 
		,(11,'CHRG_OFF_A','C',0,0) 
		,(12,'SETT_AUTH','C',0,0) 
		,(13,'NAME','C',0,0) 
		,(14,'D_SUFFIX','C',0,0) 
		,(15,'PATIENT_NM','C',0,0) 
		,(16,'D_EMP_STREET','C',0,0)
		,(17,'D_EMP_STREET2','C',0,0) 
		,(18,'D_EMP_CITY','C',0,0) 
		,(19,'D_EMP_STATE','C',0,0) 
		,(20,'D_EMP_ZIP','C',0,0) 
		,(21,'ORIG_CRED','C',0,0) 
		,(22,'FRST_DT_DELQ','C',0,0) 
		,(23,'AGENCY_COMM','C',0,0) 
		,(24,'PALL_ACCTNO','C',0,0) 
		,(25,'ORIG_ACCTNO','C',0,0) 
		,(26,'D_PHYSICIAN','C',0,0) 
		,(27,'D_EMP','C',0,0)
		,(28,'FACILITY_NM','C',0,0)
		,(29,'DIAG_CD','C',0,0)
	) d (SD_KEY, SD_TYPE, EDIT_SECURITY, MULTI_RECORDS, OtherCol)
)
,MaxVal(SD_KEYMax)
AS
(
	SELECT MAX(SD_KEY) 
	FROM SDMAST WITH (UPDLOCK, SERIALIZABLE)
)
INSERT INTO SDMAST
SELECT SD_KeyMax + SD_KEY, SD_TYPE, EDIT_SECURITY, MULTI_RECORDS, OtherCol
FROM NewRows
	CROSS JOIN MaxVal;
Go to Top of Page

Chris_Kelley
Posting Yak Master

102 Posts

Posted - 08/19/2014 :  12:51:28  Show Profile  Reply with Quote
tkizer = Got it, I changed the values to +1,+2 etc and it worked perfect.


Ifor = That is some beautiful coding, I am going to study it and work to become that awesome...

Thanks everyone


Thanks,
Chris
Jr Programmer
Go to Top of Page

Ifor
Aged Yak Warrior

590 Posts

Posted - 08/19/2014 :  13:08:33  Show Profile  Reply with Quote
Umm...
The coding is a kludge as the underlying design looks awful.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000