Update and left outer join statements Update and left outer join statements sql-server sql-server

Update and left outer join statements


Update t SET        t.Column1=100FROM        myTableA t LEFT JOIN        myTableB t2 ON        t2.ID=t.ID

Replace myTableA with your table name and replace Column1 with your column name.

After this simply LEFT JOIN to tableB. t in this case is just an alias for myTableA. t2 is an alias for your joined table, in my example that is myTableB. If you don't like using t or t2 use any alias name you prefer - it doesn't matter - I just happen to like using those.


If what you need is UPDATE from SELECT statement you can do something like this:

UPDATE suppliers    SET city = (SELECT customers.city FROM customersWHERE customers.customer_name = suppliers.supplier_name)


Just another example where the value of a column from table 1 is inserted into a column in table 2:

UPDATE  AddressSET     Phone1 = sp.PhoneFROM    Address ad LEFT JOIN Speaker spON      sp.AddressID = ad.IDWHERE   sp.Phone <> ''