• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: SQL Functions Can Do Non-function Things

    January 27, 2020 Ted Holt

    We all know that the SQL SELECT statement only retrieves data, right? If you want to modify data, you have to use INSERT, UPDATE, DELETE, or MERGE, correct? Well, to quote the great George Gershwin, It Ain’t Necessarily So. You can modify data from a SELECT statement, and maybe sometimes you should. Here’s how it’s done.

    I’ll illustrate with a SELECT statement that will run the Reorganize Physical File (RGZPFM) command over physical files that have at least 10 percent deleted records. I could do this with plain ol’ CL, of course, and that’s probably the approach I would take if I had to do this task for real. But I need a simple example that you can run on your system, so this fits the bill.

    First, I need the pertinent information for all the physical file members in a library. The Display File Description (DSPFD) command can do that.

    DSPFD FILE(SOMELIB/*ALL) TYPE(*MBRLIST) +
       OUTPUT(*OUTFILE) OUTFILE(QTEMP/FD)
    

    If you prefer to use SQL to retrieve this data, take a look at the SYSPARTITIONSTAT catalog view.

    Second, I need an SQL function that can reorganize a physical file member.

    create or replace function mylib.ReorgPF
      (p_Library varchar(10), p_File varchar(10), p_Member varchar(10))
      returns integer
      modifies sql data
      returns null on null input
      begin
         declare v_Command   varchar(80);
         declare exit handler for sqlexception
            begin return 0; end;
         set v_command = 'RGZPFM FILE(' concat trim(p_Library) concat
                                    '/' concat p_File    concat
                               ') MBR(' concat p_Member  concat ')';
         call qsys2.qcmdexc(v_Command);
         return 0;
      end;
    

    Does something strike you as odd? It should. A function is a routine that, given zero or more values, returns a value. This function hardly lives up to that ideal. What we have here is a stored procedure masquerading as a function.

    Notice the MODIFIES SQL DATA clause. If you omit that clause, the system nips the reorg in the proverbial bud.

    Last, I need a query that selects the members that have at least 10% deleted records. (Specifying 9.5 instead of 10 is one way to round.)

    select MLLIB,MLFILE,MLNAME,MLNRCD,MLNDTR,
            (MLNDTR/(MLNRCD + MLNDTR)),
            ReorgPF(f.MLLIB,f.MLFILE,f.MLNAME)
      from qtemp.fd as f
     where MLNDTR > 0
       and (MLNDTR/(MLNRCD + MLNDTR)) >= .095;
    

    Notice the last column. It executes the function for each selected file member. Here’s my test data.

    MLLIB MLFILE MLNAME MLNRCD MLNDTR Expression
    SOMELIB CUSTOMERS CUSTOMERS 58 2 0.033333
    SOMELIB MFGORDDTL MFGORDDTL 53 7 0.116667
    SOMELIB MFGORDHDR MFGORDHDR 40 0 0
    SOMELIB PODTL PODTL 10 0 0
    SOMELIB POHDR POHDR 58 2 0.033333
    SOMELIB REPS REPS 60 0 0
    SOMELIB SHIPDTL SHIPDTL 58 2 0.033333
    SOMELIB SHIPHDR SHIPHDR 0 0 invalid
    SOMELIB SLSORDDTL SLSORDDTL 59 1 0.016667
    SOMELIB SLSORDHDR SLSORDHDR 56 4 0.066667
    SOMELIB VENDORS VENDORS 50 10 0.166667

    In this case, files MFGORDDTL and VENDORS were reorganized.

    The purist in me says that a function should never modify data. The realist says not to use the words never and always. As long as everything is open and above-board, I don’t see a problem using this type of function.

    RELATED STORIES

    SYSPARTITIONSTAT

    Guru: SQL Table Functions Can Do Non-function Things

    It Ain’t Necessarily So

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

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

    Sponsored by
    ARCAD Software

    Embrace VS Code for IBM i Development

    The IBM i development landscape is evolving with modern tools that enhance efficiency and collaboration. Ready to make the move to VS Code for IBM i?

    Join us for this webinar where we’ll showcase how VS Code can serve as a powerful editor for native IBM i code and explore the essential extensions that make it possible.

    In this session, you’ll discover:

    • How ARCAD’s integration with VS Code provides deep metadata insights, allowing developers to assess the impact of their changes upfront.
    • The role of Git in enabling seamless collaboration between developers using tools like SEU, RDi, and VS Code.
    • Powerful extensions for code quality, security, impact analysis, smart build, and automated RPG conversion to Free Form.
    • How non-IBM i developers can now contribute to IBM i projects without prior knowledge of its specifics, while ensuring full control over their changes.

    The future of IBM i development is here. Let ARCAD be your guide!

    Watch Now

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Four Hundred Monitor, January 27 DevOps Transformation: Engage Your IBM i Teams

    2 thoughts on “Guru: SQL Functions Can Do Non-function Things”

    • Rob Berendt says:
      January 27, 2020 at 9:19 am

      We created our own QCMDEXE function to do stuff like this. Sure, there’s an IBM supplied stored procedure called QCMDEXE but that pretty much requires you imbed it in SPL. Using a function bypasses that requirement

      Reply
    • Ted Holt says:
      August 25, 2020 at 11:56 am

      Bob Cozzi recommends adding the following options to the function definition if the function will execute CL commands.

      NOT FENCED
      DISALLOW PARALLEL

      The reason is that CL commands and some API’s are not thread-safe.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 30 Issue: 7

This Issue Sponsored By

  • New Generation Software
  • WorksRight Software
  • RPG & DB2 Summit
  • ARCAD Software
  • TL Ashford

Table of Contents

  • Power9 Enters The Long Tail
  • DevOps Transformation: Engage Your IBM i Teams
  • Guru: SQL Functions Can Do Non-function Things
  • Four Hundred Monitor, January 27
  • IBM i PTF Guide, Volume 22, Number 4

Content archive

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

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

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