Try OpenEdge Now
skip to main content
ABL Essentials
Using Queries : Defining and using queries : OPEN and CLOSE QUERY statements : Using an outer join in a query
 
Using an outer join in a query
An outer join between tables is a join that does not discard records in the first table that have no corresponding record in the second table. For example, consider this query definition:
DEFINE QUERY CustOrd FOR Customer, Order.

OPEN QUERY CustOrd FOR EACH Customer, EACH Order OF Customer.
As the AVM retrieves records to satisfy this query, it first retrieves a Customer record and then the first Order record with the same CustNum field. When you do a NEXT operation on the query, the AVM locates the next Order for that Customer (if there is one), and replaces the contents of the Order buffer with the new Order. If there are no more Orders for the Customer, then the AVM retrieves the next Customer and its first Order.
The question is: What happens to a Customer that has no Orders at all? The Customer does not appear in the result set for the query. The same is true for a FOR EACH block with the same record phrase. This is simply because the record phrase asks for Customers and the Orders that match them, and if there is no matching Order, then the Customer by itself does not satisfy the record phrase.
In many cases this is not the behavior you want. You want to see the Customer data regardless of whether it has any Orders or not. In this case, you can include the OUTER-JOIN keyword in the OPEN QUERY statement:
DEFINE QUERY CustOrd FOR Customer, Order.

OPEN QUERY CustOrd FOR EACH Customer, EACH Order OF Customer OUTER-JOIN.
Now the AVM retrieves Customers even if they have no Orders. When the Customer has no Orders, the values for all fields in the Order buffer have the Unknown value (?).