| Author |
Topic |
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-19 : 18:58:30
|
| Hi, I have 2 tables:Table 1: ProductIDorderIDDescriptionTable2:ProdIDordIDselect *from dbo.table1 left outer join dbo.table2 on ProdID = ProductIDIs the left outer join correct? I want 2 things.1) I want is that if the orderID is null in table1 then fill it with values from table2 if it exists.2) so basically update orderId with value from table2 if the value of orderId is null or empty in table1SA |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-19 : 19:22:23
|
| I want all records from table2 into table1 based on the following condition:If the Table2.orderId is null => replace it with table1.ordIDI am not sure if it should be inner or outer? Please adviceSA |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-19 : 20:02:37
|
| Thank you Tara! One more question related to these 2 tables itself. Table 1: (new master table)ProductIDorderIDDescriptionTable2: (Legacy table - old data which needs to be transferred to the new table)ProdIDordIDSo I need to check table2:ProdID and ordID against table1:ProductID and orderID. If these values do not exists in table1 (ie new master table) then insert a new row with this value else do nothing.Thanks again.SA |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-19 : 20:14:36
|
| Let me test it out. Thanks for your help!!SA |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-20 : 17:27:39
|
| The query listed below works well but there is one more question. I also need to check if the t1.ProductID = t2.ProdID AND t1.TransID = t2.ordID. Please note that orderID and TransID is mapped to ordID in the TABLE 2. How would I perform this complicated query. There could be some overlap. THe total rows is only 1386 but I get 418 rows from first insert and 992 rows from the second insert. There is an overlap of 24 rows. Is this how you do this with 2 inserts or is there another approach to combine these 2 queries.Table 1: (new master table)ProductIDorderID -- orderID and TransId is mapped to ordID in the TABLE 2.TransIDDescriptionTable2: (Legacy table - old data which needs to be transferred to the new table)ProdIDordID-- inserts 418 rowsINSERT INTO Table1 (ProductID, orderID)SELECT ProdID, ordIDFROM Table2 t2WHERE NOT EXISTS (SELECT * FROM Table1 t1 WHERE t1.ProductID = t2.ProdID AND t1.orderID = t2.ordID)--inserts 992 rowsINSERT INTO Table1 (ProductID, TransID)SELECT ProdID, ordIDFROM Table2 t2WHERE NOT EXISTS (SELECT * FROM Table1 t1 WHERE t1.ProductID = t2.ProdID AND t1.TransID = t2.ordID)SA |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-20 : 19:07:43
|
| I tried this query and it gives me 24 rows ie the overlap rows as suggested in my questionSA |
 |
|
|
|