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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 INSERT statement conflicted

Author  Topic 

Mingebag
Starting Member

4 Posts

Posted - 2013-04-03 : 04:30:29
Heey guys I'm have some strange trouble's that I don't know why they appear .To simplify my problem I just want to merge values from tow tables together so this is not a hard task but I have a FOREIGN KEY that is make some trouble with this key I can see which user has added some vacation (Nothing special a normal task of a FOREIGN KEY ) and I want to give this FOREIGN key the values of the old one but somehow this is not working there is always the same error message

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AnnualVacation_HtUser1". The conflict occurred in database "HolidayTracker", table "dbo.HtUser", column 'UserId'.


My query
INSERT INTO [ch-s-0008180].HolidayTracker.dbo.AnnualVacation(UserId,FromDate,ToDate,WorkingTime,VacationDays)
SELECT
u1.user_htUserId ,
CAST('2013-01-01' AS DATETIME),
CAST('2013-12-31' AS DATETIME),
u1.vacationDays,
u1.workingTime
FROM HolidayTracker.dbo.AnnualVacation u1
LEFT JOIN [ch-s-0008180].HolidayTracker.dbo.AnnualVacation u2
ON u1.user_htUserId = u2.UserId AND
u1.workingTime = u2.workingTime AND
u1.vacationDays = u2.vacationDays


Thanks for help and fast answer !!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-03 : 04:38:16
checked if values passed for UserId is a valid one existing in master table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Mingebag
Starting Member

4 Posts

Posted - 2013-04-03 : 04:44:23
quote:
Originally posted by visakh16

checked if values passed for UserId is a valid one existing in master table.
OO thanks for you advice i see the UserId are different so can I some how Map the Key is there a solution for this problem ?
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-03 : 04:46:37
you need to insert those values into master table before you do above insert. Thats only solution if you want to preserve the foreign key

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Mingebag
Starting Member

4 Posts

Posted - 2013-04-03 : 04:51:13
But I already have added all Users in this table the PK can never be the same ...
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-04-03 : 06:52:36
well the error means that the value for UserID you're trying to insert into the table does not exist in HolidayTracker.dbo.HtUser table. You may like to validate as if the entering userids exist in HtUser table?

Cheers
MIK
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-03 : 07:07:57
quote:
Originally posted by Mingebag

But I already have added all Users in this table the PK can never be the same ...


Did you add with same id values?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Mingebag
Starting Member

4 Posts

Posted - 2013-04-03 : 07:26:36
quote:
Originally posted by visakh16

quote:
Originally posted by Mingebag

But I already have added all Users in this table the PK can never be the same ...


Did you add with same id values?

Nope they have different ID's and that is why it is not possible, but is there another way to reach the same result just via email or something like that ?
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-03 : 07:38:37
nope..no way. so far as FK is defined it should have same id values inserted in table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -