Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

UPDATE table1 with table2 data Expand / Collapse
Author
Message
Posted Tuesday, March 29, 2011 9:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 24, 2011 10:00 AM
Points: 19, Visits: 105
Just wondering if this is the best way to do this update statement:

UPDATE [table1]
SET [field2] =
(SELECT [field2] FROM [table2] WHERE [table2].[field1] = [table1].[field1])

If you would write this statement another way, please do post.

Thanks,
Michal.
Post #1085655
Posted Tuesday, March 29, 2011 10:02 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 1:58 PM
Points: 367, Visits: 615
Could also be:

UPDATE t1
SET t1.[field2] = t2.[field2]
FROM [table1] t1
INNER JOIN [table2] t2 NO t1.[field1] = t2[field1]

Eliminates the subquery. Should be more efficient on large datasets.
Post #1085684
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse