• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • SQL PL Conditional Structures

    October 18, 2016 Ted Holt

     Recently I gave a brief introduction to the SQL Procedures Language, or SQL PL, a procedural language that works with the DB2 family of database management systems. Today I present the conditional structures, IF and CASE. If you so choose, you will master them in minutes.

    But first, I need to clear up one matter. In my previous article, I referred to SQL PL as a proprietary language. Mike Cain, of the DB2 for i Center of Excellence, emailed to correct my error. Mike pointed out that SQL PL is based on SQL/Persistent Stored Modules (SQL/PSM), which is an ISO standard. Therefore, SQL PL is not proprietary, but a standard language that other DBMS vendors have so far not chosen to implement. There is a difference, and Mike is correct.

    The bottom line, however, remains the same: SQL PL runs on DB2 only.

    Having corrected my poor choice of words, I turn to the matter at hand–making decisions.

    SQL PL has two decision-making structures, and they are very easily learned.

    IF

    The IF structure has three forms:

    * IF — THEN — END IF
    * IF — THEN — ELSE — END IF
    * IF — THEN — ELSEIF — THEN — ELSE — END IF

    I won’t explain the logic, since they work identically to their RPG counterparts. However, the placement of semicolons is different, so let’s look at that.

    * Do not place semicolons after THEN and ELSE.
    * Place one statement after THEN and ELSE. That statement may be a simple statement or a compound statement.
    * Terminate each simple statement, including simple statements within compound statements, with a semicolon.
    * Terminate END IF with a semicolon only when something follows it.

    Let’s look at a couple of examples.

    This trigger fires before an item is added to the item master table. If the stocking unit of measure is kilograms, the trigger ensures that the weight is either positive or null.

    create trigger ItemInsert
       no cascade
       before insert on items
       referencing new row as n
       for each row
       mode db2row
    if n.Stocking_UOM = 'KG' and n.weight <= 0 then
       signal sqlstate '86100'
          set Message_text = 'Weight must be positive or null';
    end if
    

    If I try to insert an item with a weight of -4 kilograms, the system prohibits the insertion and returns SQL state 86100. Notice that there is no semicolon after the END IF, since nothing follows.

    Here is a beefier version of the previous example, showing nested IF and the ELSE statement.

    create trigger iteminsert
       no cascade
       before insert on items
       referencing new row as n
       for each row
       mode db2row
    if Stocking_UOM = 'KG' then
       if n.weight <= 0 then
          signal sqlstate '86100'
             set Message_text = 'Weight must be positive or null';
       end if;
    else
       signal sqlstate '86199'
          set Message_text = 'Invalid unit of measure';
    end if
    

    CASE

    The CASE structure, like the CASE expression that you probably use in SELECT statements, has two forms–a simple form and a searched form:

    * CASE value WHEN — ELSE — END CASE
    * CASE WHEN — ELSE — END CASE

    If you’re not familiar with simple and searched CASE, I recommend that you read this article by Skip Machesani. It deals with the CASE expression, but the explanation of simple and searched CASE applies to the CASE structure as well.

    Be aware that the SQL PL CASE structure and the CASE expression differ in at least three ways:

    * The CASE expression returns a value, as if it were a function. The SQL PL CASE is a control structure.
    * SQL PL does not require ELSE, but if no WHEN matches and there is no ELSE, the CASE fails with SQLSTATE 20000. The CASE expression returns a null when no WHEN matches and there is no ELSE.
    * You must use END CASE, not END, to terminate the SQL PL CASE. You can terminate the CASE expression with either END or END CASE, but if you add a correlation name, as I usually do, you have to terminate the CASE expression with END.

    Here’s the same trigger, beefed up a bit more than before.

    create trigger ItemInsert
       no cascade
       before insert on items
       referencing new row as n
       for each row
       mode db2row
    case
       when Stocking_UOM = 'KG' then
          if n.weight <= 0 then
             signal sqlstate '86100'
                set Message_text = 'Weight must be positive or null';
          end if;
       when Stocking_UOM in ('CM', 'M') then
          if n.length <= 0 then
             signal sqlstate '86100'
                set Message_text = 'Length must be positive or null';
          end if;
       else
          signal sqlstate '86199'
             set Message_text = 'Invalid unit of measure';
    end case
    

    IF and CASE are not difficult to learn. I can’t think of any way the designers of SQL PL could have made them any easier.

    Ted Holt welcomes your comments and questions. Email him through the IT Jungle Contacts page.

     

    RELATED STORIES

    A Brief Introduction to the SQL Procedures Language

    A Case for CASE

    SQL Procedures, Triggers, and Functions on IBM DB2 for i

     

    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

    Chrono-Logic:  Deploy automatically to multiple IBM i and Windows servers with a single click!! Fresche:  IBM i staffing for all of your IT needs. Request a FREE estimate. 1-800-361-6782 Manta Technologies Inc.:  The Leader in IBM i Education! Download catalog and take sample sessions!

    Mobile Apps As Easy As RPG III On Your IBM i Radar Now: GDPR

    Leave a Reply Cancel reply

Volume 16, Number 23 -- October 18, 2016
THIS ISSUE SPONSORED BY:

T.L. Ashford
WorksRight Software
Focal Point Solutions Group

Table of Contents

  • A Style Guide for Modern RPG and ILE, Part 2
  • Replacing Source in The Twenty-First Century
  • SQL PL Conditional Structures

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