"Ian Boyd" <ia***********@avatopia.com> wrote in message
news:cg********@enews2.newsguy.com...
i know nothing about DB2, but i'm sure this must be possible.
i'm trying to get a client to create a view (which it turns out is called
a "Logical" in DB2). The query needs a LEFT OUTER JOIN, but he doesn't know
how to do that, or even if he can, and i don't have to time to learn DB2
from scratch right now.
The following SQL Query is a trimmed sample of the full View (i.e.
Logical) definition - and i would create it on an SQL based databse engine:
SELECT
CustomerMaster.SYIDCM, --[KeyField]
CustomerMaster.FN@CM, --First name
CustomerMaster.MI@@CM, --Middle Initial
CustomerMaster.LN@@CM, --Lastname
CustomerBusinessAddress.SYIDCIC, --[Key Join Field]
CustomerBusinessAddress.MAILCA, --Mail Code
CustomerBusinessAddress.AD1@CA, --Business Address 1
CustomerBusinessAddress.AD2@CA, --Business Address 2
CustomerBusinessAddress.CTY@CA --Business City
FROM CSPCM CustomerMaster
LEFT OUTER JOIN CSPCA CustomerBusinessAdress
ON CustomerMaster.SYIDCM = CustomerBusinessAddress.SYIDCA
AND CustomerBusinessAddress.MAILCA = 'B'
Part of the full logical definition he gave me is:
JDFTVAL
R CMS2 JFILE(CSPCM CSPCX CSPCA +
CSPCP CSPCIC)
J JOIN(CSPCM CSPCX)
JFLD(SYIDCM SYIDCX)
J JOIN(CSPCM CSPCA)
JFLD(SYIDCM SYIDCA)
J JOIN(CSPCM CSPCP)
JFLD(SYIDCM SYIDCP)
J JOIN(CSPCM CSPCIC)
JFLD(SYIDCM SYIDCIC)
SYIDCM JREF(1)
FNM@CM
MI@@CM
LNM@CM
SEX@CM
...
Now be being a guy who grew up with SQL, this syntax is...horrible - but i
can sorta see what's happening.
Isn't there a syntax to do outer joins? So that instead of
J JOIN(CSPCM CSPCA)
JFLD(SYIDCM SYIDCA)
JFLD(MAILCA 'B')
it's
LJ JOIN(CSPCM CSPCA)
JFLD(SYIDCM SYIDCA)
JFLD(MAILCA 'B')
Where LJ is left join?
or maybe
J JOIN(CSPCM CSPCA)
LEFTJFLD(SYIDCM SYIDCA)
LEFTJFLD(MAILCA 'B')
Like i said, i have NO idea what this syntax is; i'm just guessing.
Is a LEFT JOIN join possible in DB2?
You haven't said which version of DB2 you are using or what platform you are
on. Your examples don't look like SQL from any of the platforms I have used
except possibly AS/400 which I used for a few days once several years (and
version of DB2) ago. The answer to virtually *any* DB2 question depends on
your DB2 version and your OS so I can only make some general remarks.
Left joins are possible on the last few versions of DB2 on
Windows/Unix/Linux/OS/2 and also on OS/390. I don't recall if they are
available on AS/400. Although I used DB2 for VM for a while in its first
release, that was in the mid 1980s and I haven't seen it since; I don't know
if left joins are available in DB2 for VM/VSE today.
You can find the manuals for DB2 on all platforms (except AS/400) at
http://www-306.ibm.com/software/data...nfocenters.jsp. You'll
need to track down the AS/400 manuals yourself at the IBM website. Try a
search on "AS/400 DB2" at http://www.ibm.com.
The manual that contains the rules of SQL syntax for DB2 is called SQL
Reference on Windows/Unix/Linux and OS/390. I'm not sure if the same applies
to VM/VSE and AS/400.
For what it's worth, I've never called a "view" a "logical" in 20 years of
using DB2. I've occasionally described a view as a "logical table" but
normally I just call it a "view".
Also, the query at the beginning of your question -
SELECT CustomerMaster.SYIDCM, --[KeyField]
CustomerMaster.FN@CM, --First name
CustomerMaster.MI@@CM, --Middle Initial
CustomerMaster.LN@@CM, --Lastname
CustomerBusinessAddress.SYIDCIC, --[Key Join Field]
CustomerBusinessAddress.MAILCA, --Mail Code
CustomerBusinessAddress.AD1@CA, --Business Address 1
CustomerBusinessAddress.AD2@CA, --Business Address 2
CustomerBusinessAddress.CTY@CA --Business City
FROM CSPCM CustomerMaster
LEFT OUTER JOIN CSPCA CustomerBusinessAdress
ON CustomerMaster.SYIDCM = CustomerBusinessAddress.SYIDCA
AND CustomerBusinessAddress.MAILCA = 'B'
- looks perfectly fine to me. The FROM clause ought to work on any platform
that supports Left Joins just the way it is written here.
The "logical definition" you have given doesn't look like anything I've ever
seen before. However, if this is AS/400, it may be perfectly valid. I've
always found AS/400 the most "different" of the DB2 platforms.
You'll want to look at the CREATE VIEW statement in your platform's SQL
Reference to see the exact syntax for creating a view.
Lastly, it's been my experience that users tend to mess up joins if left to
do them on their own. (Not *all* users write bad joins but quite a few seem
to fall into this category). They often forget the joining condition
entirely or sometimes choose the wrong columns to join. Unless you plan to
approve their queries somehow after the users have written them, you may
find that it is better for you to create views for them that include
properly-written joins, then let the users built their queries against the
views rather than against the base tables. Just a thought....
Rhino