• 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

    DevSecOps & Peer Review – The Power of Automation

    In today’s fast-paced development environments, security can no longer be an afterthought. This session will explore how DevSecOps brings security into every phase of the DevOps lifecycle—early, consistently, and effectively.

    In this session, you’ll discover:

    • What DevSecOps is and why it matters?
    • Learn how to formalize your security concerns into a repeatable process
    • Discover the power of automation through pull requests, approval workflows, segregation of duties, peer review, and more—ensuring your data and production environments are protected without slowing down delivery.

    Whether you’re just getting started or looking to enhance your practices, this session will provide actionable insights to strengthen your security posture through automation and team alignment to bring consistency to the process.

    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

  • What You Will Find In IBM i 7.6 TR1 and IBM i 7.5 TR7
  • Three Things For IBM i Shops To Consider About DevSecOps
  • Big Blue Converges IBM i RPG And System Z COBOL Code Assistants Into “Project Bob”
  • As I See It: Retirement Challenges
  • IBM i PTF Guide, Volume 27, Number 41
  • Stacking Up Power11 Entry Server Performance To Older Iron
  • Big Blue Boosts IBM i Support In Instana, Adds Tracing
  • It Is Time To Tell Us What You Are Thinking And Doing
  • IBM i PTF Guide, Volume 27, Number 40
  • The GenAI Boom Is Only Slightly Louder Than The Dot Com Boom

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