13
Wed, Nov
5 New Articles

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

CL
Typography
  • 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                    

                          +2'PAGE:'                 

                          +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.

Pgm                                                  

                                                      

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) +      

                       NewObj(MyResults))                  

                                                                      

           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 +                

                           '%'')')                            

             EndDo                                          

                                                              

           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 +              

                          &Status_Chr)                      

             EndDo                                          

                                                              

           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 +    

                           &Year_Chr)                      

             EndDo                                        

                                                          

           ChgVar Var(&SQLText) +                          

                 Value(&SQLText *BCat +                  

                 'order by Class, EffDate)')          

                                                          

           RunSQL SQL(&SQLText) Commit(*None)              

                                                          

           CallSubr Subr(ReadFile)                        

           ClrPFM File(QTemp/MyResults)                    

EndDo                                                      

                                                            

Return                                                    

                                                          

Subr       Subr(ChkWhere)                                  

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

               ChgVar Var(&SQLText) +                      

                     Value(&SQLText *BCat 'Where')        

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

               EndDo                                        

Else       Cmd(ChgVar Var(&SQLText) +                    

                       Value(&SQLText *BCat 'and'))        

EndSubr                                                    

                                                            

Subr       Subr(ReadFile)                                  

OvrDBF     File(SampleX) ToFile(QTemp/MyResults)          

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

                                                            

OpnFCLF   FileID(MyReportX) Usage(*Output) +            

             LvlChk(*No)                                    

WrtRcdCLF FileID(MyReportX) RcdFmt(Heading) +            

             RcdBuf(&NoDtaToWrt)                        

                                                        

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) +                    

                       Value(&MyResults_Class)          

               ChgVar Var(&Sts_Desc) +                

                       Value(&MyResults_Sts_Desc)      

               ChgVar Var(&EffDate) +                  

                     Value(&MyResults_EffDate)        

               WrtRcdCLF FileID(MyReportX) +          

                         RcdFmt(Detail) +              

                         RcdBuf(&RptLine)              

               RtvFInfCLF FileID(MyReportX) +          

                           CurPrtLine(&CurPrtLin) +    

                           PrtFOvrFlw(&OvrFlwLin)      

                                                        

               If Cond(&CurPrtLin *GE &OvrFlwLin) +    

                   Then(WrtRcdCLF FileID(MyReportX) +  

                                 RcdFmt(Heading) +    

                                 RcdBuf(&NoDtaToWrt))  

               EndDo                                  

             Else Cmd( +                                

                 WrtRcdCLF FileID(MyReportX) +        

                           RcdFmt(End_Of_Rpt) +        

                           RcdBuf(&NoDtaToWrt))        

EndDo                                                  

                                                        

CloFCLF   FileID(MyReportX)                          

Close     OpnID(MyResults)  

DltOvr     File(SampleX)    

EndSubr                        

                                

EndPgm                              

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..

Bruce Vining

Bruce Vining is president and co-founder of Bruce Vining Services, LLC, a firm providing contract programming and consulting services to the System i community. He began his career in 1979 as an IBM Systems Engineer in St. Louis, Missouri, and then transferred to Rochester, Minnesota, in 1985, where he continues to reside. From 1992 until leaving IBM in 2007, Bruce was a member of the System Design Control Group responsible for OS/400 and i5/OS areas such as System APIs, Globalization, and Software Serviceability. He is also the designer of Control Language for Files (CLF).A frequent speaker and writer, Bruce can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it.. 


MC Press books written by Bruce Vining available now on the MC Press Bookstore.

IBM System i APIs at Work IBM System i APIs at Work
Leverage the power of APIs with this definitive resource.
List Price $89.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: