2

I am new to this language and having some problems understand queries.

for example:

There are 2 database tables: Warehouse, product. So each warehouse can have multiple products and products can be stored in different warehouses.

Query:
   for each warehouse,
       each product:
   display warehouse.name, product.prodcode.
end.

the display will like

warehousename   productcode
awarehouse       SKA-301

so for this result, are these columns display total independent result, eg. SKA-301 product may not in awarehouse. Or it will display the product in awarehouse? what if product and warehouse don't have related fields?

Please help me. Thank you.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
Simoncat
  • 21
  • 2

2 Answers2

4

In the code that you have shown you will get each product for every iteration of warehouse.

To get the products that are specific to a particular warehouse you need to add WHERE criteria to the 2nd clause of the join. Assuming that you have a product.warehouseName field that would suit this purpose....

for each warehouse no-lock,
  each product no-lock where product.warehouseName = warehouse.name:

  display
    warehouse.name
    product.prodcode
  .

end.

(If there is no index on product.warehouseName this will be very inefficient.)

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
  • You could add that "query:" above the for each statement is a label, useful with NEXT for instance. Unsure if you actually can (you obviously shouldn't) use that keyword as label though. – Jensd Aug 26 '13 at 19:55
  • Actually, "QUERY" can not be used as a label for a loop. – Jensd Aug 27 '13 at 06:40
0

Your query is compact version of this expanded version

for each warehouse:
   for each product:
      display warehouse.name, product.prodcode. 
   end.
end.

They both achieve the same thing. I would suggest since you are starting off to expand the query out and once you understand the relationships then go back to the compact each, each version

TerryB
  • 629
  • 1
  • 5
  • 13