datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to update with correlated subquery?

Execute the following Microsoft SQL Server T-SQL scripts in SSMS Query Editor to perform updates with a correlated subquery or a JOIN respectively:

 
USE AdventureWorks;
-- SQL update with correlated subquery - UPDATE with SELECT subquery
UPDATE d
SET    Name = (SELECT Name
               FROM   BackupOfAdventureWorks.HumanResources.Department
               WHERE  DepartmentID = d.DepartmentID)
FROM   HumanResources.Department d;
 
GO
 
-- SQL update with join - SQL Server update with inner join
UPDATE d
SET    d.Name = bd.Name
FROM   HumanResources.Department d
       JOIN BackupOfAdventureWorks.HumanResources.Department bd
         ON bd.DepartmentID = d.DepartmentID;
 

GO

SQL subquery definition: A subquery is a query that is nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. When a subquery has a reference to a table in the outer query, it is called correlated subquery.

 

-- SQL subquery

SELECT soh.SalesOrderID, OrderDate=convert(varchar, soh.OrderDate, 111),

    (SELECT MAX(sod.UnitPrice)

     FROM AdventureWorks.Sales.SalesOrderDetail AS sod) AS MaxUnitPrice

FROM AdventureWorks.Sales.SalesOrderHeader AS soh

ORDER BY OrderDate

GO

/* Partial results

 

SalesOrderID      OrderDate   MaxUnitPrice

43697             2001/07/01  3578.27

43698             2001/07/01  3578.27

43699             2001/07/01  3578.27

*/

 

-- SQL correlated subquery – reference to soh.SalesOrderID

SELECT soh.SalesOrderID, OrderDate=convert(varchar, soh.OrderDate, 111),

    (SELECT MAX(sod.UnitPrice)

     FROM AdventureWorks.Sales.SalesOrderDetail AS sod

     WHERE soh.SalesOrderID = sod.SalesOrderID) AS MaxUnitPrice

FROM AdventureWorks.Sales.SalesOrderHeader AS soh

ORDER BY OrderDate

GO

/* Partial results

 

SalesOrderID      OrderDate   MaxUnitPrice

43659             2001/07/01  2039.994

43660             2001/07/01  874.794

43661             2001/07/01  2039.994

*/
------------

 
-- UPDATE example with correlated subqueries and GROUP BY
UPDATE AdventureWorks2008.Sales.SalesPerson
SET SalesYTD = SalesYTD +
    (SELECT SUM(soh1.SubTotal)
     FROM AdventureWorks2008.Sales.SalesOrderHeader AS soh1
     WHERE soh1.OrderDate = (SELECT MAX(OrderDate)
                             FROM AdventureWorks2008.Sales.SalesOrderHeader AS soh2
                             WHERE soh2.SalesPersonID = soh1.SalesPersonID)
     AND Sales.SalesPerson.BusinessEntityID = soh1.SalesPersonID
     GROUP BY soh1.SalesPersonID);
-- (17 row(s) affected)

------------
 

Related articles:

http://sqlusa.com/articles2005/updatesyntax/

UPDATE (Transact-SQL)

SQL Question: Update using correlated subquery?

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.
TOP PAY JOBS SEARCH PAGES
SQL DEVELOPER BUSINESS INTELLIGENCE SSRS SSIS SSAS OLAP
MICROSOFT ORACLE COGNOS SAP IBM GOOGLE FACEBOOK
HEALTHCARE OFFICE WEB SUPPORT COMPUTER FINANCE MEDIA
NURSE HOSPITAL THERAPIST PHYSICIAN PHARMACIST MEDICAL DRIVER
SALES MARKETING HOLLYWOOD MUSIC VIDEO SOCIAL MEDIA SOFTWARE
MBA AD-PR BIOTECH EDUCATION HR TRAVEL FOOD TECHNOLOGY
FEDERAL GOVERNMENT OFFSHORE GREEN CARD N DAKOTA COLLEGE SCHOOL REP
WORK-AT-HOME TELECOMMUTE OPENINGS HIRING MEDICAL-BILLING NURSING PART

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.