Skip to content
Advertisement

Remove duplicates and fix foreign keys in T-SQL

I have the following tables:

Now I need to insert all Rows of Foo_1 into Foo_2 while removing duplicates.

This is done using this code

Now I have

And now I need to create the table Bar_2 which should look like the following

But how can I find out the correct new Foreign Key value for BarId?

Advertisement

Answer

You should be able to match Bar_1.BarId to Foo_1.Id to get the Foo_1.Dupe to match Foo_2.Dupe to get the Foo_2.Id.

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