I have difficulties solving a problem. I'll try to explain the problem the best I can. I got two tables: Table A and Table B. Due to some poor programming the tables are not normalized and this is what's causing my problem. Table A:- Field A ID (Unique)- TableB.FieldA (Foreign key)- Field X- Field Y- Field zetc(Field A and Table B ID is a joint primary key)Table B:- Field A (Old unique key)- Field B (Foreign key)- Field C (new unique key)(Field A and Field B ID is a joint primary key)Due to poor programming Field A and Field B were used as a joint primary key, but conceptually only Field A should be a primary key. Because of that the it sometimes happen that the same key in field A appears multiple times so I had to make a new primary key in Field C with unique keys.Table A uses TableB.FieldA as a foreign key. Multiple rows in Table A can have the same Foreign Key. But because I had to make a new unique key in Table B (FieldC), those data in Table A with the same foreign key also belongs to the new keys made in TableB.FieldC. Example: quote: Table BField A Field B Field C------- ------- -------1..........700.......90001..........701.......9001 (ID 1 appears 2 times in Field A. Not allowed)2..........700.......90023..........700.......9003Table AField A Field B (from TableB.FieldA) Field C------- ---------------------------- ---------1..........1........................................Some text2..........1........................................Some text3..........1........................................Some text4..........2........................................Some text5..........2........................................Some text6..........3........................................Some text7..........3........................................Some text- 1 -
As you see in the first quote ID 1 appears 3 times as Foreign key in TableA.FieldB. In Table B you see that ID 1 got two new ID's (9000 and 9001). That means that for every row in Table A with the old ID 1 I have to insert two new rows for ID 9000 and 9001. And that's my problem. How do I duplicate the rows in Table A based on the number of new ID's made in TableB.FieldC?Don't be afraid ask question to clarify the problem if needed. It was difficult to write down the problem so I don't know if I got the problem well explained. I want the result to be like in the code-quote below:quote: Table AField A Field B (from TableB.FieldA) Field C------- ---------------------------- ---------1..........1........................................Some text2..........1........................................Some text3..........1........................................Some text4..........9001...................................Some text5..........9001...................................Some text6..........9001...................................Some text7..........2........................................Some text8..........2........................................Some text9..........3........................................Some text10.........3........................................Some textRows in bold are new rows
Code--Create and populate temp tablesif object_id('tempdb..#tableA', 'U') is not null drop table #tableAif object_id('tempdb..#tableB', 'U') is not null drop table #tableBcreate table #tableB ( FieldA int not null ,FieldB int not null ,FieldC int )alter table #tableB add constraint pk_tableA primary key clustered ( FieldA ,FieldB )create table #tableA ( FieldA int not null ,FieldB int not null ,FieldC varchar(100) null )alter table #tableA add constraint pk_tableB primary key clustered (FieldA)--Put some data in there.insert #tableB(FieldA, FieldB, FieldC) select '1','700','9000' union allselect '1','701','9001' union allselect '2','700','9002' union allselect '3','700','9003' insert #tableA(FieldA, FieldB, FieldC)select '1','1','some text' union allselect '2','1','some text' union allselect '3','1','some text' union allselect '4','2','some text' union allselect '5','2','some text' union allselect '6','3','some text' union allselect '7','3','some text' |