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

    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

  • 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
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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