Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help structuring an UPDATE statement with multiple joins

Status
Not open for further replies.

iRead

Programmer
Mar 12, 2004
25
US
Okay I have an application where I need to replace a variable in one data table with the value from another data table. Problem is there are two table in between that relates the records and I can’t figure out how to structure the update statement.

I use a join like the one below to return a record set.

SELECT Table_1.Var_1, Table_1.Var_2, Table_2.Var_3, Table_2.Var_4, Table_2.Var_5, Table_3.Var_6, Table_3.Var_7, Table_3.Var_8, Table_4.Var_9, Table_4.Var_10 FROM Table_1, Table_2, Table_3, Table_4 WHERE Table_1.Var_2 = Table_2.Var_3 AND Table_2.Var_4 = Table_3.Var_6 AND Table_2.Var_5 = Table_3.Var_7 AND Table_3.Var_8 = Table_4.Var_9

I need to then replace Var_1 in Table_1 with Var_10 from Table_4 for all of the records returned by the SELECT statement. The SELECT statement works perfectly. I’ve worked on the update for a while and sadly have made no significant headway. Any suggestions and help will be greatly appreciated.

Thank you!
iRead
 
How does this work for you?

Code:
UPDATE Table_1
SET Var_1 = T4.Var_10
FROM Table_1 T1
   INNER JOIN Table_2 T2 ON T1.Var_2 = T2.Var_3
     INNER JOIN Table_3 T3 ON T2.Var_4 = T3.Var_6
                       AND T2.Var_5 = T3.Var_7
         INNER JOIN Table_4 T4 ON T3.Var_8 = T4.Var_9

--Angel [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
AngelTampaBay,

There was a filter condition I neglected to put in the select statement in my posting. Soon as I included that in the update statement you suggested and tweeked it a little it seems to work just as intended.

Thank you greatly!
iRead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top