|
|||||||
|
|
![]() |
|
|
|
|
||
|
Query Management and Outer Joins Hey, Ted: We have used Query/400 to generate a significant number of queries to which we pass parameters at runtime. We have a good technique, but there is one problem I can't figure out. Here is the technique we use.
To run the query, we use the Start Query Management Query (STRQMQRY) command, passing the parameters to the query from a selection screen through the SETVAR parameter. However, this technique fails us when we specify a join type of 2 when building the Query/400 query. Join type 2 causes the query to select all records from the primary file, even if there are no matching records in the secondary files. The generated QM query drops unmatched primary-file records as if we had specified join type 1 when we created the Query/400 query. Is there a way to select unmatched primary records using the technique I've described? --Rich Good question, Rich! Yes, you can fix this. When you tell Query Management to retrieve the SQL from a Query/400 query, it joins the files in the WHERE clause, like this:
H QM4 05 Q 01 E V W E R 01 03 03/03/03 22:28
V 1001 050 My Query/400 query converted to QM
V 5001 004 *HEX
SELECT
ALL T01.SHIPNBR, T01.COMPANY, T01.CUSTNBR, T01.DATE, T01.PRONBR
T02.COMPANY, T02.CUSTNBR, T02.CUSTNAME, T02.CADDRESS1,
T02.CADDRESS2, T02.CCITY, T02.CSTATE, T02.CZIP, T02.CREDITL
T02.TSTAMP
FROM MYLIB/SHIPMENTS T01,
MYLIB/CUSTOMER T02
WHERE T01.COMPANY = T02.COMPANY
AND T01.CUSTNBR = T02.CUSTNBR
AND( T01.DATE BETWEEN &FROMDATE AND &THRUDATE)
You need to change it to use the JOIN clause, like this: FROM SHIPMENTS T01 LEFT OUTER JOIN CUSTOMER T02 ON T01.COMPANY = T02.COMPANY AND T01.CUSTNBR = T02.CUSTNBR WHERE T01.DATE BETWEEN &FROMDATE AND &THRUDATE A left outer join is what you're using when you specify join type 2 in Query. The join criteria have been moved from the WHERE clause to the ON clause. The record selection criterion remained in the WHERE clause, since it has nothing to do with joining the files. You can also specify the right outer join, the left and right exception joins, the inner join, and the cross join. The inner join and the left exception join are like Query/400 join types 1 and 3, respectively. --Ted
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |