In SQL Server 2016 I have a staging table [S] with 8 columns. I want to copy that data to 3 tables [A], [B] and [C]. [A] has FK to [B] and [B] has FK to [C]. Columns 1-2 are to go to [c], column 3 to [a] and columns 4-8 to [B].
Can anyone tell me how I can do this? Efficiency is not paramount as only 5.5Krows.
Any help much appreciated.
edit:
Should have made it clearer:
- Tables a,b and c exist and have PK and FK's in place.
- c has a unique constraint on a concatination of col1 and col2.
- a,b, and c use the IDENTITY PK
- Data to be imported has been cleansed of duplicates.
So effectively you're splitting up a row from S over 3 tables? Why not use primary key from table S instead of "chained" key you want to use?
You'd still be able to join the data the way you want, you would also be able to join tables A and C without using B.
p.s. If you don't have a PK in S, or is in a shape you don't want to copy, you can still use a surrogate key in one table and reference it from other two.
here's a solution for table C, you can adjust it for other tables:
or
or (too implicit for my taste)
edit: If you're trying to get rid of redundancies then you could use DENSE_RANK() like this (add/remove rank for keys you need):