Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
TableA has 2 columns ColumnA, columnB2845, A2845, B2845, C2848, A 2848, BI need to populate TableB which has only one column(PK) Col1 which is Auto generated PK12345and TableC which is a junction between TableA and TableB(PK) (FK) (FK)Colum1, Colum2, Column3, colum41 2845 1 A2 2845 2 B3 2845 3 C4 2848 4 A5 2848 5 BMy destination TableC should look like aboveHow do I accomplish this in SQL?Thanks
sodeep
Master Smack Fu Yak Hacker
7174 Posts
Posted - 2009-02-06 : 22:36:06
Why you need junction table over here?
ssiss
Starting Member
10 Posts
Posted - 2009-02-06 : 23:01:49
quote:Originally posted by sodeep Why you need junction table over here?
I need a junction table, because TableB has several other columns and primary key of TableB is used as foreign key in several other tables.
PeterNeo
Constraint Violating Yak Guru
357 Posts
Posted - 2009-02-06 : 23:38:30
u r requirement is not clear, but try this
SELECT A.ColumnA , B.Col1 , A.ColumnBFROM ( SELECT *, ROW_NUMBER() OVER( ORDER BY ColumnA, ColumnB) AS 'RowSeq' FROM TableA ) AINNER JOIN TableB B ON A.RowSeq = B.Col1
"There is only one difference between a dream and an aim.A dream requires soundless sleep to see,whereas an aim requires sleepless efforts to achieve..!!"
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-02-07 : 09:07:24
isnt it just a matter of adding an two columns in tableC and populating them using ROW_NUMBER() function as Peter pointed out?