访问数据库循环 - 为一个表中的每条记录创建另一个表中的记录数组

问题描述:

是否可以在中创建嵌套循环查询访问数据库将更新第三个表?访问数据库循环 - 为一个表中的每条记录创建另一个表中的记录数组

我有一个主(头)表:

------------------------ 
masters 
------------------------ 
num | modality | cost | 
------------------------ 
01 | thing | 23.00 | 
02 | thing | 42.00 | 
03 | thing | 56.00 | 
04 | apple | 11.00 | 
05 | apple | 17.00 | 

而且我需要创建第三个(实际)细节含临时表详细信息表将键关机的的主人

这里是一个temp细节表的示例。

---------------------------------- 
temps 
---------------------------------- 
modelnumber | modality | priceEa | 
---------------------------------- 
| 123  | thing | 1.00 | 
| 234  | apple | 2.00 | 
| 345  | apple | 3.00 | 
| 456  | apple | 4.00 | 
| 567  | thing | 5.00 | 

基本上,我需要遍历在大师表中的每个记录。

外环:

对于主人表中的每个记录,抢模式。

内环:

然后在临时工表,其中的方式匹配的每个记录,创建详细信息表中的记录(以及在这个过程中,做基于temps.priceEa和主人一些计算。成本)。

这应该在主表中为每个记录在详细信息表中创建(主数据*临时数)新记录数。

细节表,最终应该看起来像

---------------------------------------------------------- 
details 
---------------------------------------------------------- 
num | modelnumber | modality | priceEa | adjustedCost | 
---------------------------------------------------------- 
| 01 | 123   | thing  | 1.00 | (do calc here) 
| 01 | 567   | thing  | 5.00 | (do calc here) 
| 02 | 123   | thing  | 1.00 | (do calc here) 
| 02 | 567   | thing  | 5.00 | (do calc here) 
| 03 | 123   | thing  | 1.00 | (do calc here) 
| 03 | 567   | thing  | 5.00 | (do calc here) 
| 04 | 234   | apple  | 2.00 | (do calc here) 
| 04 | 345   | apple  | 3.00 | (do calc here) 
| 04 | 456   | apple  | 4.00 | (do calc here) 
| 05 | 234   | apple  | 2.00 | (do calc here) 
| 05 | 345   | apple  | 3.00 | (do calc here) 
| 05 | 456   | apple  | 4.00 | (do calc here) 
...etc 

 

SELECT m.num, t.modelnumber, m.modality, t.priceea 
into myNewTempTable 
from masters m inner join temp t on m.modality = t.modality 
order by m.num, t.modelnumber 
 
+0

我想你didz吧!有趣的是,有时sql执行循环更新/插入操作并不像它会这样做......这让我的大脑以错误的方式思考问题。谢谢。 – m42 2009-04-30 19:36:20