Re: Run query based on values in another table
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Thu, 29 Mar 2007 07:22:00 +0000 (UTC)
bubbles (bubbles.one@xxxxxxxxxxx) writes:
Newbie transiting from VBA to TSQL, using SQL Server 2005 Enterprise:
Need help to do this:
Open Table_A
WITH TableA
DO UNTIL .EOF
Read value from TableA.ColumnA
Run SQL Statement on TableB based on value
Move to the next record
LOOP
END
How do I do this in TSQL?
It is not unlikely that this is a single SQL statement, but it depends on
what operation you intend do to on TableB. Assuming that the operation
is "Give me the total order sum for each customer", the query in Northwind
is:
SELECT C.CompanyName, SUM(OD.Quantity * OD.UnitPrice)
FROM [Order Details] OD
JOIN Orders O ON OD.OrderID = O.OrderID
JOIN Customers C ON O.CustomerID = C.CustomerID
GROUP BY C.CompanyName
A very important lesson of this is that the mindset in SQL is completely
different from a client language.
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.
- Follow-Ups:
- Re: Run query based on values in another table
- From: bubbles
- Re: Run query based on values in another table
- References:
- Run query based on values in another table
- From: bubbles
- Run query based on values in another table
- Prev by Date: Run query based on values in another table
- Next by Date: Re: Run query based on values in another table
- Previous by thread: Run query based on values in another table
- Next by thread: Re: Run query based on values in another table
- Index(es):
Relevant Pages
|