Understanding the Sample Program for Master and Detail Reports

In the sample program, nested queries are invoked once for each customer, and each one retrieves records that correspond to the current customer. A bind variable correlates the subqueries in the WHERE clause. This variable correlates the customer number (cust_num) with the current customer record:

Program ex7a.sqr
begin-program
  do main
end-program
begin-procedure main
begin-select
  Print 'Customer Information' (,1)
  Print '-'                    (+1,1,45) Fill
name   (+1,1,25)
city   (,+1,16)
state  (,+1,2)
cust_num
  do cash_receipts(&cust_num)
  do orders(&cust_num)
  position (+2,1)
from customers
end-select
end-procedure ! main
begin-procedure cash_receipts (#cust_num)
  let #any = 0
begin-select
  if not #any
     print 'Cash Received' (+2,10)
     print '-------------' (+1,10)
     let #any = 1
  end-if
date_received    (+1,10,20) edit 'DD-MON-YY'
amount_received  (,+1,13) Edit $$$$,$$0.99
from cash_receipts a
where a.cust_num = #cust_num
end-select
end-procedure ! cash_receipts
begin-procedure orders (#cust_num)
  let #any = 0
begin-select
  if not #any
    print 'Orders Booked' (+2,10)
    print '-------------' (+1,10)
    let #any = 1
  end-if
a.order_num
order_date                (+1,10,20) Edit 'DD-MON-YY'
description               (,+1,20)
c.price * b.quantity      (,+1,13) Edit $$$$,$$0.99
from  orders a, ordlines b, products c
where a.order_num = b.order_num
  and b.product_code = c.product_code
  and a.cust_num = #cust_num
end-select
end-procedure ! orders
begin-heading 3
 print $current-date (1,1) Edit 'DD-MON-YYYY'
 page-number (1,69) 'Page ' 
end-heading