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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 INSERT statement conflicted
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Mingebag
Starting Member

4 Posts

Posted - 04/03/2013 :  04:30:29  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 04/03/2013 :  04:38:16  Show Profile  Reply with Quote
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 - 04/03/2013 :  04:44:23  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 04/03/2013 :  04:46:37  Show Profile  Reply with Quote
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 - 04/03/2013 :  04:51:13  Show Profile  Reply with Quote
But I already have added all Users in this table the PK can never be the same ...
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1052 Posts

Posted - 04/03/2013 :  06:52:36  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 04/03/2013 :  07:07:57  Show Profile  Reply with Quote
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 - 04/03/2013 :  07:26:36  Show Profile  Reply with Quote
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

India
52309 Posts

Posted - 04/03/2013 :  07:38:37  Show Profile  Reply with Quote
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
  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.14 seconds. Powered By: Snitz Forums 2000