logo

Wednesday 08th of September 2010

News

Latest from Directory

New Links

Home Database MsSql Update Join MsSql
Update Join MsSql PDF Print E-mail
Written by AlmaSeo   
Wednesday, 18 November 2009 22:12

In some cases we need to update a column in a table according to a join on another table. Making all in a single statement on Microsoft SQL Server is not easy, let's see how we can work it out.

Suppose we have a table of TERRITORIES and have to fill the column REGION DESCRIPTION according to the value retrieved from the second table REGIONS in join on the RegionID field

 

 

 

Territories table "TERRITORIES"

Territories

 

Region table "REGIONS"

 

Regions

 

the query toperform the update in the join is the following:

 

UPDATE TERRITORIES
SET REGIONDESCRIPTION= B.
REGIONDESCRIPTION
FROM TERRITORIES , REGIONS B
WHERE
TERRITORIES .REGIONID= B.REGIONID


and here it is the result:


TerritoriesUpdated

 

ingenious, isn't it?

Last Updated on Thursday, 19 November 2009 18:43
 


Powered by Joomla!. Designed by: web host famous people Valid XHTML and CSS.