|
Posted by Erland Sommarskog on 03/29/07 07:22
bubbles (bubbles.one@hotmail.com) 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@sommarskog.se
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
Navigation:
[Reply to this message]
|