The CL Corner: Using Multiple Files with the RUNSQL CL Command

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times


Take advantage of being able to join files with SQL.


Last month, in "Run-Time Selection Using the RUNSQL CL Command," we saw how to build an SQL statement on the fly, selecting specific records/rows from the file/table SAMPLE. In this article, we'll look at how to combine information across files using the RUNSQL CL command. But before combining information, we first need to define that information, which will be accessed external to the file SAMPLE.

In the file SAMPLE, there is a 1-digit numeric field named Status, with records within SAMPLE currently having Status values of 1 or 2. As the values of 1 and 2 are not, in and of themselves, too descriptive, let's create another file, named STATUSVALS, which contains brief descriptive text describing Status values. This file will contain two fields: Status and Sts_Desc (Status Description). Using DDS, the definition of STATUSVALS might be as shown below.

R RECORD                

  STATUS         1S 0  

  STS_DESC     10A    

K STATUS                


Alternatively, you could create the STATUSVALS file using the RUNSQL command and the SQL CREATE TABLE statement. An example of using CREATE TABLE, rather than DDS, can be found in the previous article "Introducing the New Run SQL Command." The CREATE TABLE example shown in the referenced article is for SAMPLE, but it should be sufficient for you to determine the equivalent for the STATUSVALS file.

Having created STATUSVALS, using either DDS or SQL, we now need to write (or insert) three records/rows. This can be accomplished using the RUNSQL CL commands shown below.

RUNSQL SQL('Insert into STATUSVALS Values(1, ''Active'') ') Commit(*None)

RUNSQL SQL('Insert into STATUSVALS Values(2, ''Proposed'') ') Commit(*None)

RUNSQL SQL('Insert into STATUSVALS Values(3, ''Inactive'') ') Commit(*None)

While we're at it, let's also add a new record to the file SAMPLE. This record will have a Status value that is not defined in the STATUSVALS file that we just wrote three records to. This new SAMPLE record will have a Class of 'BAD', a Status of 4, and an EffDate of December 31, 9999. It can be written using the following RUNSQL command.

RUNSQL SQL('Insert into SAMPLE Values(''BAD'', 4, ''9999-12-31'') ') + Commit(*None)  

With the database setup work out of the way, let's now look at a report we would like to generate.

7/27/2012               MY SAMPLE REPORT               15:51:38 PAGE:   1

   CLASS STATUS           EFF. DATE                                      

   A     Active           05/01/2012                                        

   ABC   Proposed         04/15/2012                                        

   BAD   **UNKNOWN         12/31/9999

   FIRST Active           04/13/2012                                        

   3rd   Proposed         06/01/2012                                        

                       ** END OF REPORT **                                

This report enhances the report we were generating in last month's article. The enhancement is that, rather than a numeric Status value being printed, a textual description of the status is provided. In addition, if an unknown Status value is encountered, then the descriptive text '**UNKNOWN' will be printed. To accommodate the longer text associated with the Status column of the report, we need to make some minor adjustments to the MYREPORT printer file. Below, with the changes bolded, are the necessary changes.

R HEADING                                           

                        2  2DATE(*SYS *YY) EDTCDE(Y)

                        2 28'MY SAMPLE REPORT'      

                        2 59TIME                    


                          +1PAGNBR EDTCDE(3)        

                        4  5'CLASS'                 

                        4 12'STATUS'                

                        4 30'EFF. DATE'             

R DETAIL                    SPACEB(1)               

  CLASS          5         5                        

  STS_DESC      10        12                        

  EFFDATE         L       30DATFMT(*USA)            

R END_OF_RPT                SPACEB(2)               

                          26'** END OF REPORT **'   

Assuming that you have stored the previous printer file source in member MYREPORTX of source file QDDSSRC, you can create the printer file with the command CRTPRTF MYREPORTX QDDSSRC.

Similar to how we needed to change the printer file, we will also need to change the definition of the intermediate file we populate with the RUNSQL statement. The DDS for the new file, SAMPLEX, is shown below. The only change from the previous file SAMPLE is related to changing the second field, a 1-digit numeric Status field, to a 10-byte character Sts_Desc field. To create the physical file SAMPLEX into QGPL, you could use the command CRTPF QGPL/SAMPLEX QDDSSRC.

R RECORD            

  CLASS          5A 

  STS_DESC      10A 

  EFFDATE         L 

The updated RPTSAMPLE CL program, which generates the previous report, is shown below with changes again shown in bold.



DclF       File(MyDSPF)                              

DclF       File(SampleX) OpnID(MyResults)            

Dcl       Var(&EOF)       Type(*Lgl)                


Dcl       Var(&RptLine)   Type(*Char) Len(25)      

Dcl       Var(&Class)     Type(*Char) Len(5) +      

               Stg(*Defined) DefVar(&RptLine 1)      

Dcl       Var(&Sts_Desc) Type(*Char) Len(10) +    

               Stg(*Defined) DefVar(&RptLine 6)      

Dcl       Var(&EffDate)   Type(*Char) Len(10) +    

               Stg(*Defined) DefVar(&RptLine 16)      


Dcl       Var(&NoDtaToWrt) Type(*Char) Len(1)        

Dcl       Var(&OvrFlwLin) Type(*Dec) Len(3 0)      

Dcl       Var(&CurPrtLin) Type(*Dec) Len(3 0)            


Dcl       Var(&SQLText)   Type(*Char) Len(1024)          

Dcl       Var(&Where)     Type(*Lgl)                      

Dcl       Var(&Status_Chr) Type(*Char) Len(1)              

Dcl       Var(&Year_Chr)   Type(*Char) Len(4)              


DoWhile   Cond(*not &IN03)                                

           SndRcvF RcdFmt(Rpt_Prompt)                      


           If Cond(&IN03) Then(Leave)                      


           ChkObj Obj(QTemp/MyResults) ObjType(*File)      

           MonMsg MsgID(CPF9801) Exec( +                    

             CrtDupObj Obj(SampleX) FromLib(*Libl) +      

                       ObjType(*File) ToLib(QTemp) +      



           ChgVar Var(&SQLText) Value( +                            

                   'Insert into QTemp/MyResults +                    

                     (Select Class, +                              

                             coalesce(Sts_Desc, ''**UNKNOWN''), +

                             EffDate +                            

                       from Sample s +                              

                           left join StatusVals sv +                

                            on sv.Status = s.Status')              


           ChgVar Var(&Where) Value('0')                            


           If Cond(&Rqs_Class *NE ' ') Then(Do)                      

             CallSubr Subr(ChkWhere)                                

             ChgVar Var(&SQLText) +                                

                     Value(&SQLText *BCat +                          

                           'Class like(''' *TCat +                

                           &Rqs_Class *TCat +                




           If Cond(&Rqs_Status *NE 0) Then(Do)                

             CallSubr Subr(ChkWhere)                        

             ChgVar Var(&Status_Chr) Value(&Rqs_Status)      

             ChgVar Var(&SQLText) +                          

                     Value(&SQLText *BCat +                  

                           's.Status = ' *BCat +              




           If Cond(&Rqs_Year *NE 0) Then(Do)                  

             CallSubr Subr(ChkWhere)                        

             ChgVar Var(&Year_Chr) Value(&Rqs_Year)          

             ChgVar Var(&SQLText) +                          

                     Value(&SQLText *BCat +                

                          'Year(EffDate) = ' *BCat +    




           ChgVar Var(&SQLText) +                          

                 Value(&SQLText *BCat +                  

                 'order by Class, EffDate)')          


           RunSQL SQL(&SQLText) Commit(*None)              


           CallSubr Subr(ReadFile)                        

           ClrPFM File(QTemp/MyResults)                    





Subr       Subr(ChkWhere)                                  

If         Cond(*not &Where) Then(Do)                    

               ChgVar Var(&SQLText) +                      

                     Value(&SQLText *BCat 'Where')        

               ChgVar Var(&Where) Value('1')                


Else       Cmd(ChgVar Var(&SQLText) +                    

                       Value(&SQLText *BCat 'and'))        



Subr       Subr(ReadFile)                                  

OvrDBF     File(SampleX) ToFile(QTemp/MyResults)          

ChgVar     Var(&EOF) Value('0')                          


OpnFCLF   FileID(MyReportX) Usage(*Output) +            


WrtRcdCLF FileID(MyReportX) RcdFmt(Heading) +            



DoUntil   Cond(&EOF = '1')                          


             RcvF OpnID(MyResults)                      

           MonMsg MsgID(CPF0864) Exec( +              

               ChgVar Var(&EOF) Value('1'))            


             If Cond(&EOF *EQ '0') Then(Do)            

               ChgVar Var(&Class) +                    


               ChgVar Var(&Sts_Desc) +                


               ChgVar Var(&EffDate) +                  


               WrtRcdCLF FileID(MyReportX) +          

                         RcdFmt(Detail) +              


               RtvFInfCLF FileID(MyReportX) +          

                           CurPrtLine(&CurPrtLin) +    



               If Cond(&CurPrtLin *GE &OvrFlwLin) +    

                   Then(WrtRcdCLF FileID(MyReportX) +  

                                 RcdFmt(Heading) +    



             Else Cmd( +                                

                 WrtRcdCLF FileID(MyReportX) +        

                           RcdFmt(End_Of_Rpt) +        




CloFCLF   FileID(MyReportX)                          

Close     OpnID(MyResults)  

DltOvr     File(SampleX)    




For the most part, the changes are the replacement of references to SAMPLE to now use SAMPLEX, references to MYREPORT to now use MYREPORTX, and references to the numeric field Status to now use the character field Sts_Desc. The actual SQL statement being run, however, does have quite a few changes.

Where we previously had a SQL statement beginning with

'Insert into QTemp/MyResults +    

  (Select * +                    

    from Sample')

we now have this statement:

'Insert into QTemp/MyResults +                    

(Select Class, +                              

         coalesce(Sts_Desc, ''**UNKNOWN''), +

         EffDate +                            

   from Sample s +                              

         left join StatusVals sv +                

           on sv.Status = s.Status')              

The previous SQL 'Select * from Sample' indicated that we wanted all fields of SAMPLE to be inserted into the temporary file MYRESULTS. All fields in this case meant Class, Status, and EffDate. The new SQL has quite a bit more to say.

The "from" clause specifies that for all records/rows read from file SAMPLE (which is also to be known within the context of the SQL statement by the correlation name "s"), then the value of the SAMPLE Status field is to be used to also access (join) all records in file STATUSVALS (known by the correlation name "sv ") with the same value in the Status field of STATUSVALS. Because we have specified a left join, if no record in STATUSVALS is found for the Status value found in SAMPLE (for instance, a SAMPLE Status value of 4), then NULL values are to be returned for the fields that would normally be found in STATUSVALS (for our purposes, the field Sts_Desc). The actual records of SAMPLE that are to be processed is controlled by the "where" clause generated later in the RPTSAMPLE program (and introduced last month).

The use of the coalesce built-in function in the "Select" clause specifies that if a NULL value/expression is found for the field Sts_Desc, then the text '**UNKNOWN' should be returned. This is what enables the Class BAD record of the SAMPLE file to be shown in our report with a status description of **UNKNOWN. Class BAD has a Status value of 4, and there is no record in STATUSVALS with a Status value of 4.

A correlation name (in the "from" clause, the "s" following the file name SAMPLE and the "sv" following the file name STATUSVALS) is used to remove possible ambiguity when referencing the field Status. As Status exists in both of the files SAMPLE and STATUSVALS, there are cases (in particular the "where" processing associated with &Rqs_Status) where we need to clearly identify the Status field we are interested in. The correlation names s and sv are also used with the "on" clause of the left join specification, though other approaches (not correlation-based) could have been used.

This month, we have seen how to use the RUNSQL command to join multiple files in one command, how to write the resulting records of the join to an intermediate file, and how to then process the intermediate fileā€”all from a CL program. For some of you, this capability might be used to replace or enhance current OPNQRYF processing; for others, perhaps new application opportunities that can be done by one of our favorite languages: CL.

More CL Questions?

Wondering how to accomplish a function in CL? Send your CL-related questions to me at This email address is being protected from spambots. You need JavaScript enabled to view it..