Reply to Re: Run query based on values in another table

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация