• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Database Output from CL

    June 7, 2006 Hey, Ted

    I know that IBM added a lot of new programming features to CL in the past two releases. However, one of the features I’ve been hoping for–the ability to do output operations to database files–doesn’t appear to be among them. Please tell me that I’m wrong and that there really is a way to update a database record within a CL program.

    –David

    You’re right in thinking that IBM has not added CL commands to write, delete, and update database records, David, but that does not mean that there is no way to update a record in a database file.

    Let me back up and say that CL has always had a limited ability to write to the database. Every command that includes an OUTPUT parameter and allows the *OUTFILE option writes to a database file. This uses Display Object Description (DSPOBJD) command:

    DSPOBJD OBJ(THOLT/S*)  OBJTYPE(*PGM) +
               OUTPUT(*OUTFILE) OUTFILE(QTEMP/DSPOBJD)
    

    Even if a command does not allow output to an outfile, you may still be able to route printed data into a database file. The following sequence of commands places the output of the Display Library List (DSPLIBL) command into program-described file LIBL in QTEMP.

    OVRDBF  FILE(QPRTLIBL) TOFILE(QTEMP/LIBL) LVLCHK(*NO)
    CRTPF   FILE(QTEMP/LIBL) RCDLEN(132)
    DSPLIBL OUTPUT(*PRINT)
    

    You can also use Copy File (CPYF) and Copy from Query File (CPYFRMQRYF) commands to add to or replace the contents of a database file. (Did you see the tip called Where’s the Other MBROPT Option? I got some good responses to that one.)

    So CL has always had some ability to update and add to database files, but this does not address your question, which is how to update a single record in a database file.

    I suggest you look at Qshell’s db2 utility, which allows you to run SQL commands. When I first wrote about the db2 utility, IBM did not officially support it. As far as I know, that is still the case. My reasoning for that is that db2 is not listed among the utilities in the V5R4 Qshell reference.

    The db2 utility cannot process a cursor, so there is no way for you to use Receive File (RCVF) to read a record, then use db2 to update same the record, as you can do using SQL in a high-level language like RPG or COBOL. You will have to put enough expressions in the WHERE clause to isolate the record you want to update. If you can’t isolate the record, db2 won’t do the job for you and you’ll have to resort to another language.

    In the following example that I quickly threw together, the program attempts to read a record whose ID field has the value INVOICE. If that record is found, the program adds one to the batch sequence number and updates the record. If the record is not found, the program creates a record with a key value of INVOICE.

    pgm                                                             
                                                                    
      dcl  &Command    *char    256                                 
      dcl  &BatchNbrA  *char      5                                 
      dcl  &Lib        *char     10                                 
                                                                    
      dclf BatchSeq                                                 
      
      /* Don't present a Qshell terminal session. */ 
      addenvvar  envvar(QIBM_QSH_CMD_OUTPUT)     value('NONE')      
      monmsg     cpfa980                                            
                                                   
      /* Position to the INVOICE record in BatchSeq. */ 
      ovrdbf  file(BatchSeq) position(*keyae 1 rec 'INVOICE')       
      rcvf                                                          
      monmsg  cpf4137                                               
                                                      
      /* The db2 command needs to know which library the file is in. */
      rtvobjd   obj(BatchSeq) objtype(*file) rtnlib(&lib)           
                                                                    
      /* Add or update the INVOICE record with the last batch number. */
      if (&ID *eq 'INVOICE') do                                     
         chgvar &BatchNbr   (&BatchNbr + 1)                         
         chgvar &BatchNbrA  &BatchNbr                               
         chgvar &Command    ('update' *bcat &lib *tcat '.BatchSeq +
                                 set BatchNbr=' *cat &BatchNbrA *cat +         
                             ' where ID=''INVOICE''')                          
      enddo                                                                    
      else do                                                                  
         chgvar &BatchNbr   1                                                  
         chgvar &BatchNbrA  &BatchNbr                                          
         chgvar &Command    ('insert into' *bcat &lib *tcat '.BatchSeq +       
                                values(''INVOICE'',' *cat &BatchNbrA *cat ')') 
      enddo                                                                    
                                                                               
      chgvar &Command ('db2 "' *cat &Command *tcat '"')                        
      qsh    cmd(&Command)
    

    The generated db2 commands look something like this:

    QSH CMD('db2 "update MYLIB.BatchSeq set BatchNbr=00003 
    		where ID=''INVOICE''"')
    
    QSH CMD('db2 "insert into MYLIB.BatchSeq 
    		values(''INVOICE'',00001)"')
    

    Before you decide whether or not to use the db2 utility, you need to be aware of a problem. This command always returns a zero status, whether the command executes properly or not. Such behavior does not do wonders for error-checking, and so far I have not found a good solution to it.

    –Ted

    RELATED STORIES

    Where’s the Other MBROPT Option?

    Updating Through an SQL Cursor

    Access the Database from Qshell

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Bytware:  Network security, anti-virus, monitoring, notification/alerts, file recovery, & compliance
    nuBridges:  Leading provider of secure FTP on the iSeries
    COMMON:  Join us at the Fall 2006 conference, September 17-21, in Miami Beach, Florida

    OCEAN User Group to Host Annual Tech Conference on July 11 Recursive Queries on the iSeries and System i

    Leave a Reply Cancel reply

Volume 6, Number 22 -- June 7, 2006
THIS ISSUE SPONSORED BY:

WorksRight Software
Advanced Systems Concepts
DataMirror

Table of Contents

  • Database Output from CL
  • V5R4 Security: Rochester Rests Not on Its Laurels,
  • Admin Alert: Preparing Your i5 Shop for a Pandemic

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle