Question

Is it possible to create a nested looping query in Access DB that will update a third table?

I have a master (header) table:

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

and a temporary table containing detail info that I'll need to create a third (actual) details table which will key off of the masters table

here's a sample of the temp details table.

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

Basically, I need to loop through every record in the masters table.

Outer loop:

For each record in the masters table, grab the modality.

Inner loop:

Then for each record in the temps table, where the modalities match, create a record in the details table (and in the process, do some calculations based on temps.priceEa and masters.cost).

This should create (masters * temps) number of new records in the details table for every record in the masters table.

the details table, should end up looking like

----------------------------------------------------------
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
Was it helpful?

Solution


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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top