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.
| Author |
Topic |
|
mkp2004
Starting Member
8 Posts |
Posted - 2004-06-01 : 23:12:19
|
| I have 2 tables. Table 1 emp_id Duplicated_emp_id103 501103 502103 509205 717205 689316 890316 790316 280316 999 table 2 (Main Table)emp_id emp_name emp_address103 Manu adffsdfsd205 anu dfsdfsdfs316 vinu rttretreFirst step is to check whether there is a corressponding emp_id in table 2second is to duplicate as shown below.The data gets duplicated for each emp_id 103,205,316 but the data remains the same when duplicated and only the emp_id changes.Can I do a Dts or is there a way to write a script that the final output looks like shown below.Now I need to duplicate the data set for each duplicate_emp_id.the final table should look like...emp_id emp_name emp_address103 Manu adffsdfsd501 Manu adffsdfsd502 Manu adffsdfsd509 Manu adffsdfsd205 anu dfsdfsdfs717 anu dfsdfsdfs689 anu dfsdfsdfs316 vinu rttretre890 vinu rttretre790 vinu rttretre280 vinu rttretre999 vinu rttretrePlease help me out this... |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-06-01 : 23:52:06
|
| This might be the worst thing I've ever written. (grin) It's late and I'm foggy brained. It will get the job done though.DECLARE @table1 TABLE(emp_id INT, duplicate_emp_id INT)INSERT @table1(emp_id, duplicate_emp_id) SELECT 103, 501 UNION ALL SELECT 103, 502 UNION ALL SELECT 103, 509 UNION ALL SELECT 205, 717 UNION ALL SELECT 205, 689 UNION ALL SELECT 316, 890 UNION ALL SELECT 316, 790 UNION ALL SELECT 316, 280 UNION ALL SELECT 316, 999DECLARE @table2 TABLE(emp_id INT, emp_name VARCHAR(55), emp_address VARCHAR(55))INSERT @table2(emp_id, emp_name, emp_address) SELECT 103, 'Manu', 'adffsdfsd' UNION ALL SELECT 205, 'anu', 'dfsdfsdfs' UNION ALL SELECT 316, 'vinu', 'rttretre'SELECT a.emp_id, c.emp_name, c.emp_addressFROM ( SELECT emp_id FROM @table1 UNION SELECT duplicate_emp_id AS emp_id FROM @table1) a INNER JOIN @table1 b ON a.emp_id = b.emp_id OR a.emp_id = b.duplicate_emp_id INNER JOIN @table2 c ON b.emp_id = c.emp_idGROUP BY b.emp_id, a.emp_id, c.emp_name, c.emp_addressORDER BY b.emp_id, c.emp_name, c.emp_addressMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-06-02 : 09:03:09
|
| select emp_id, emp_name, emp_addressfrom table2union allselect table1.duplicated_emp_id, table2.emp_name, table2.emp_addressfrom table1inner join table2on table1.emp_id = table2.emp_idbasically the same as Derrick's method, maybe slightly shorter.- Jeff |
 |
|
|
|
|
|
|
|