Question

For Iseries/IBMi DB2.

I am joining multiple files/tables together.

I have written the code in both DDS and SQL.

The DDS Logical File is working exactly as expected, but I can not use it for embedded sql in rpgle as it then defaults to the SQE engine resulting in horrendous performance.

The SQL view, on the other hand had NULLs until I used IFNULL( MBRDESCR, ''). But now MBRDECSR is a VARCHAR. Which is unacceptable.

So how do I create a SQL join without NULLs and VARCHARs?

Requested Sample Code:

DDS:

                                            JDFTVAL                                              
                R TRANSR                    JFILE(TRANSPF MBRPF)                                

                J                           JOIN(1 2)
                                            JFLD(MBRID MBRID)                                  
      *                                                
                  TRANSID                   JREF(1)     
                  MBRID                     JREF(1)                               
                  MBRNAME                   JREF(2)                               
                  MBRSURNME                 JREF(2)                               
      *                   
                K TRANSID                                                       
                K MBRID

SQL:

CREATE VIEW TRANSV01 AS (                 
    SELECT TRANSID                                 ,
           MBRID                                   ,                 
           CAST(IFNULL(MBRNAME  , '') as Char(20)) ,                 
           CAST(IFNULL(MBRSURNME, '') as Char(25))                  
    FROM TRANSPF
    --Member Name                                                               
    LEFT OUTER JOIN MBRPF on MBRID = MBRID     
) RCDFMT TRANSR;                                                             

Please note the following:

  1. Example above is simplified

  2. Not every MBRID in the TRANSPF has a corresponding entry in the MBRPF (ie. no referential constraint). Thus when MBRPF is joined to the TRANSPF, there will be NULL values in MBRNAME, MBRSURNME. Unless JDFTVAL or IFNULL() is used.

  3. I prefer not to have a VARCHAR, because of performance and extname() in rpgle.

  4. I prefer not to have NULL values, I do not want the pgm to have to handle them.

Was it helpful?

Solution

Assuming it's the 'Allows the null value' that you find undesirable, use a UNION. The first SELECT chooses all the rows that match, which will set the NOT NULL property for you. The second SELECT chooses all the rows that don't have a match - you provide filler fields for those.

CREATE VIEW TRANSV01 AS (                 
    SELECT TRANSID                                 ,
           MBRID                                   ,                 
           MBRNAME  ,
           MBRSURNME
    FROM TRANSPF
    --Member Name                                                               
    JOIN MBRPF on MBRID = MBRID     
  UNION
    SELECT TRANSID                                 ,
           MBRID                                   ,                 
           CAST('') as Char(20)) ,                 
           CAST('') as Char(25))                  
    FROM TRANSPF
    --Member Name                                                               
    EXCEPTION JOIN MBRPF on MBRID = MBRID     
) RCDFMT TRANSR;                                             
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top