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 |
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-04-23 : 11:18:29
|
Hi,I have a temp table with a columns such as bu_id,salesrep_id,billto_idI have 3 other lookup tables:businessunit table with bu_id,bufile_idsalesrep table with salesrep_id, salesrep_fileidbillto table with billto_id and billtofile_idThe values that are inputted into the temp table are the fileids. I need to upldate the values with the ids. There is no relationship btw the 3 lookup tables.My question is if in one update statement i can update the 3 different fields in the temp table?Currently i have 3 differenct updates an example:UPDATE TEMP_DDPL2SET TEMP_DDPL2.businessunit_id = BusinessUnit.businessunit_idFROM TEMP_DDPL2 AS Temp_DDPL2INNER JOIN dbo.BusinessUnit AS BusinessUnit ON BusinessUnit.businessunit_fileID = Temp_DDPL2.businessunit_id ThanksWhisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-23 : 11:43:32
|
Can either of the 3 ids be null in the temp table?can you show us some sample data?quote: Originally posted by collie Hi,I have a temp table with a columns such as bu_id,salesrep_id,billto_idI have 3 other lookup tables:businessunit table with bu_id,bufile_idsalesrep table with salesrep_id, salesrep_fileidbillto table with billto_id and billtofile_idThe values that are inputted into the temp table are the fileids. I need to upldate the values with the ids. There is no relationship btw the 3 lookup tables.My question is if in one update statement i can update the 3 different fields in the temp table?Currently i have 3 differenct updates an example:UPDATE TEMP_DDPL2SET TEMP_DDPL2.businessunit_id = BusinessUnit.businessunit_idFROM TEMP_DDPL2 AS Temp_DDPL2INNER JOIN dbo.BusinessUnit AS BusinessUnit ON BusinessUnit.businessunit_fileID = Temp_DDPL2.businessunit_id ThanksWhisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much.
|
 |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-04-24 : 12:53:05
|
| Hi,the ids can be null in the temp tableTemp tablebu_id,salesrep_id,billto_idB11 S1235 B4343B12 S4534 B3433B11 S43455 B6432B114 S2563 B0997businessunit table bu_id,bufile_id1 B112 B123 B134 B14salesrep table salesrep_id, salesrep_fileid1 S1235 2 S4534 3 S43455 3 S2563 billto tablebillto_id and billtofile_id1 B43432 B34333 B64324 B0997Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-24 : 13:28:30
|
| [code]UPDATE tSET t.bu_id = COALESCE(bu.bu_id,t.bu_id),t.salesrep_id = COALESCE(sr.salesrep_id,t.salesrep_id),t.billto_id = COALESCE(bt.billto_id,t.billto_id)FROM temp tLEFT JOIN businessunit buON bu.bu_id = t.bu_idLEFT JOIN salesrep srON sr.salesrep_fileid = t.salesrep_idLEFT JOIN billto btON bt.billtofile_id = t.billto_idWHERE bu.bu_id IS NOT NULLOR sr.salesrep_id IS NOT NULLOR bt.billto_id IS NOT NULL[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|