1

I have to insert a person's information into a database that is running through SQL Server, and I keep getting a foreign key constraint violation.

I am trying to insert the information into a table called Person.Person, but it says the problem occurred in a table called Person.BusinessEntityID. The BusinessEntityID column is in both tables, so do I have to update both tables at the same time for it to work?

Also I noticed the BusinessEntityID value corresponds with the row number in the database, so would it be possible to set a value equal to the row number in the table?

Thank you!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mitch
  • 118
  • 9
  • 1
    You have to put the value in the 'parent' table first. SQL Server does not (unfortunately) supported deferred constraint checking so the order is important. Also there is no magic 'row number' in SQL Server - do you mean 'generated identity column value'? If yes then see 'get value of last identity insert' or INSERT...OUTPUT. – user2864740 Mar 15 '15 at 01:10
  • Gotcha so i would have to insert the businessEntityID into the Person.BusinessEntityID and then i could insert my information into the Person.Person table? yeah thats what i was referring to. The all go in numerical order so i was curious as to what i have to do to keep that consistent with the values i add in – Mitch Mar 15 '15 at 01:16
  • First step is to find out if it really is an identity column on the parent relation - if not, find out how the numbers are previously being generated. See http://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row?rq=1 (if it isn't an identity column - or one populated with a computed column / trigger - then you'll need to do some digging). – user2864740 Mar 15 '15 at 01:17

0 Answers0