![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
From this data pull, I'll generate a list of products into a table (TableA). |
|
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, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Hide quoted text - - Show quoted text - |
#4
| |||
| |||
|
|
What I need to do is this: Every night I pull data from a likned server. This is an automated job. From this data pull, I'll generate a list of products into a table (TableA). TableB contains millions of records with the details of transactions on the products. I want to loop through TableA's values (product names) to generate and run a series of SQL statements with these value so I can generate various analyses. |
#5
| |||
| |||
|
|
First lesson in SQL: stop think in loops. The normal approach is that you handle all data at once as in the query that I showed you. There are exceptions when looping may be called for, or may be the best solution, but nothing you have said this far, indicates that this would be necessary. Since I don't know what the queries against TableB look like, I cannot assist more than with the example I provided. I forgot to mention that it runs in the Northwind database. (Which does not ship with SQL 2005, but is on SQL 2000, and can be downloaded from Microsoft.com.) -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#6
| |||
| |||
|
|
First lesson in SQL: stop think in loops. The normal approach is that you handle all data at once as in the query that I showed you. There are exceptions when looping may be called for, or may be the best solution, but nothing you have said this far, indicates that this would be necessary. Since I don't know what the queries against TableB look like, I cannot assist more than with the example I provided. I forgot to mention that it runs in the Northwind database. (Which does not ship with SQL 2005, but is on SQL 2000, and can be downloaded from Microsoft.com.) -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#7
| |||
| |||
|
|
Solved this problem using cursors. |
#8
| |||
| |||
|
|
05. Using the product list, run queries against tblCalls such that the following analyses are generated into different tables (tables created on the fly). Each product will cause about 20 tables to be created (deleted and re-created at each run of the job) with various analyses. Question: 1. Is this a good strategy? 2. How to achieve this in TSQL? |
#9
| |||
| |||
|
|
If the table indeed have different layout depending on product, you would indeed have to loop, for instance using a cursor as you discovered. But if all sets of tables have the same layout, you should probably have one single set of tables instead, and in this case there would not be any need for loops. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#10
| |||
| |||
|
|
The tables are in fact seperated by products, as each set of product- specific data are handled by different engineers. These sets of data need to be sliced and diced into various analyses, and the engineers would also need to dive into raw data for some of their work. Because these product-specific data sets are large (several million records each), I thought that perprocessing and seperating them into their respective tables would help the engineers access their analyses and data faster. |
![]() |
| Thread Tools | |
| Display Modes | |
| |