• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Using SQL With Multi-Membered Files

    October 29, 2018 Mike Larsen

    Recently I worked on a process that required me to update records in a file based on certain criteria. Sounds like a common request, right? Well, there was a twist. The file had multiple members and the update needed to consider all of them. Suddenly, a “simple” request became a bit more challenging.

    I knew of a few ways to work with multi-membered files, but I like to lay out all the options before deciding on a game plan. One method I considered was to perform an override of the file (OVRDBF command). That would certainly work, although I have to loop through all the members each time the program ran.

    Another way I could work with the members is to use an SQL alias (Figure 1).

    Figure 1. Using An Alias To Access A Member Of A File

    Exec sql
         Create alias qtemp/SalesHist_January
           for SalesHist(January);                  // file name(member name)
    
       Exec sql
         Select count(*) into :numberOfItems
          from qtemp/SalesHist_January;
    
       // drop the alias when i'm done
    
       Exec sql
         Drop alias qtemp/SalesHist_January;
    

    I created the alias in QTEMP and pointed it to the file and member. When I did that, I was able to access the records in the January member of the SalesHist file. I dropped the alias because I no longer needed it. If accessing the January member were something I had to do regularly, I would have kept it.

    Using an alias allowed me to work directly with a single member. This method would also work for my task, but I’d still have to loop through all the members. Additional research showed that I could create an alias for each member and access all members with a union all operator. That seemed more like what I wanted to do since I really wanted to process all the members in one shot. However, the SQL statement could get pretty long if there was a large number of members. Using either of these methods also required me to know the names of the members and that caused additional work.

    There was one other way I thought of to tackle this assignment. I used a logical file that was built over all members of the physical file. When I ran an SQL statement over the logical file I was able to access records from all members at one time. That was exactly what I wanted! I didn’t have to do any looping and I didn’t have to know the names of the members. A caveat to this approach is the logical file needs to be rebuilt as new members are added or you won’t be able to access the new members. I chose to go this route and the SQL statement was simple (Figure 2).

    Figure 2. Updating Records In All Members

    // I can use the logical file to update records in all members at one
    // time.
    
       Exec sql
         Update SalesHist2
          Set HeOpen = substr(HeOpen,1,10) concat 'Y' concat substr(HeOpen,12,29);
    

    Not only can you select and update records in a multi-membered file, you can also insert records into a certain member (Figure 3).

    Figure 3. Using An Alias To Insert Records Into A Member

    // To insert into a particular member, I can use an Sql alias.
    
       Exec sql
         Create alias qtemp/saleshist_mike
           for saleshist(mike);
    
       Exec sql
         Insert into qtemp/saleshist_mike
           values('RT', 1, 20, 18, 09, 15, '000050675305486',
                  'open text N 000050675305486', '01', 122.22, 22.22);
    
       Exec sql
         Drop alias qtemp/saleshist_mike;   
    

    This is very similar to the select statement I showed earlier except that I used the alias to insert a record into the Mike member of the SalesHist file.

    I’ve given you several ways to work with membered files. Each of these methods will work just fine; it’s up to you to decide which is right for you.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: FHG, Four Hundred Guru, Guru, IBM i, SQL

    Sponsored by
    Rocket Software

    Meet digital age demands while maximizing your IT investment.

    Future-proof your mission-critical applications with Rocket® Solutions for IBM® i that keep your business ahead of the curve.

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Kubernetes Container Control Comes To Power Systems Break Out Of Your RPG Comfort Zone

    3 thoughts on “Guru: Using SQL With Multi-Membered Files”

    • Rob Berendt says:
      October 29, 2018 at 8:50 am

      I take it this multi member file was not a “partitioned table”? I suspect not, as a partitioned table requires DB2 Multisystem which can be purchased for around the price of a new Power 9.

      Reply
    • Íñigo says:
      October 29, 2018 at 11:58 am

      Hi Mike, and thanks for sharing.

      I’m running 7.2 versin. And it doesn’t work.
      Is it a new feature for 7.3?

      I’ve tried with an LF that only shows some columns of the table (all of the them, ar common to all PF files included in the LF).
      And, as always, I get the common error: “The file xxxxxxx has more than one format”.

      Thanks.

      Reply
      • Mike says:
        December 29, 2018 at 10:22 am

        Hi. Sorry for the late response. The files and code I wrote for the article were done on a 7.2 system. The way I did this was to create a physical file and then added physical file members to it. Once I had that in place, I created a logical file over the physical and I was then able to access data from all members via the logical file. Are you able to try it that way on your system?
        Let me know if that works for you. If not, perhaps I can email you and try to help out.

        Reply

    Leave a Reply Cancel reply

TFH Volume: 28 Issue: 72

This Issue Sponsored By

  • LUG
  • SEA
  • UCG Technologies
  • T.L. Ashford

Table of Contents

  • Break Out Of Your RPG Comfort Zone
  • Guru: Using SQL With Multi-Membered Files
  • Kubernetes Container Control Comes To Power Systems
  • It’s Your Last Chance To Take The IBM i Marketplace Survey

Content archive

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

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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