Skip to content
Advertisement

How to copy data from one table to another when foreign key is only defined in the destination table?

Table_A schema:

Table_B schema:

Column UserId is defined as Foreign Key in Table_A.
I need to copy data from Table_B to Table_A and I used:

But I get:

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_UserId”
The conflict occurred in database “Table_A”, table “dbo.Users”, column ‘Id’.

Table Users exists of course.

FYI & BTW
This situation is due to the unfortunate fact that the SO Demo Db has no FK information.
See related question

Advertisement

Answer

Well if you really needed to get those records in you would join to the users table to ensure you had vaild userid’s:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement