• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Determine The State Of The Error

    September 13, 2016 Ted Holt

    The SQL Procedures Language (SQL PL) has an effective error-handling mechanism–condition handlers. When a statement returns a certain SQL state or a certain type of exception, the condition handler takes control. However, determining which SQL state to test for can be problematic. Here are two ways.

    Let’s start with a simple stored procedure.

    Create or replace procedure CreatePlants
    begin
       create table plants
       ( ID dec(3),  Location  varchar(16),
       primary key (ID));
    
       label on table plants is 'Plant master';
    
       insert into plants values
       (  1,  'Lost Angeles'),
       (  2,  'New Yolk'),
       (  3,  'Last Vegas');
    end
    

    What could go wrong? Well, the plants table might already exist. If so, the job log has message SQL0601 (PLANTS in MYLIB type *FILE already exists.).

    Let’s say that if the plants table already exists, you’ll ignore the error and continue with the next statement. However, to do so requires you to know the SQL state. Here’s one way to determine which SQL state to trap:

    create or replace procedure CreatePlants
    begin
       declare sqlstate   char(5)       default '00000';
       declare ErrorMsg   varchar(96);
    
       declare exit handler for sqlexception
          begin
             set ErrorMsg = 'SQLSTATE=' concat sqlstate;
             signal sqlstate '99001'
                set message_text = ErrorMsg;
          end;
    
       create table plants
       ( ID dec(3),  Location  varchar(16),
       primary key (ID));
    
       label on table plants is 'Plant master';
    
       insert into plants values
       (  1,  'Lost Angeles'),
       (  2,  'New Yolk'),
       (  3,  'Last Vegas');
    end
    

    The exit handler traps any fatal error, builds a message that includes the SQL state, and cancels the stored procedure with SQL state 99001. The job log has message SQL0438. (Message SQLSTATE=42710 returned from SIGNAL, RESIGNAL, or RAISE_ERROR.)

    I am indebted to Scott Forstie of IBM for sharing another method with me. A few months ago, he pointed out that the Run SQL Scripts tool, which I run as part of IBM i Access Client Solutions, displays the SQL state when a command ends in error. You can run the entire procedure or just the statement in question, as appropriate. This screen shot shows what I’m talking about.

     

     

    Now that you know that the failed CREATE TABLE raises SQL state 42710, you can create a continue handler.

    create or replace procedure CreatePlants
    begin
    
       declare Table_Exists condition for sqlstate '42710';
    
       declare continue handler for Table_Exists
          begin end;
    
       create table plants
       ( ID dec(3),  Location  varchar(16),
       primary key (ID));
    
       label on table plants is 'Plant master';
    
       insert into plants values
       (  1,  'Lost Angeles'),
       (  2,  'New Yolk'),
       (  3,  'Last Vegas');
    end
    

    The continue handler receives control when CREATE TABLE fails, does nothing about the error, and execution continues with the LABEL ON statement. (I have written about this technique before.)

    I’m far from a wizard with SQL PL, but the more I use it, the better I get. If you’re wondering which language to learn next, you may want to consider SQL PL.

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

     

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    Want to deliver DevOps on IBM i?

    DevOps enables your IBM i development teams to shorten the software development lifecycle while delivering features, fixes, and frequent updates that are closely aligned with business objectives. Flexible configuration options within MDChange make it easy to adapt to new workflow strategies and policies as you adopt DevOps practices across your organization.

    Learn More.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    System i Developer:  RPG & DB2 Summit - October 4-6 2016 in Chicago. Register now! BCD:  Webinar: What's Possible with PHP on IBM i. Tues., Sept. 27 at 1pm ET. Sign up now! Manta Technologies Inc.:  The Leader in IBM i Education! Download catalog and take sample sessions!

    Howdy, Partner. Welcome To The IBM i Modernization Ranch New OpenPower Servers Present Interesting IBM i Possibilities

    Leave a Reply Cancel reply

Volume 16, Number 20 -- September 13, 2016
THIS ISSUE SPONSORED BY:

WorksRight Software
ProData Computer Services
UCG Technologies

Table of Contents

  • How Do You Do That With RDi? Part 1: Copy A Source Member
  • Determine The State Of The Error
  • Generate SELECT For All Columns

Content archive

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

Recent Posts

  • Positive News From The Kyndryl Mainframe Modernization Report
  • NAViGATE, inPower 2025 On Tap for September 2025
  • Guru: WCA4i And Granite – Because You’ve Got Bigger Things To Build
  • As I See It: Digital Coup
  • IBM i PTF Guide, Volume 27, Number 37
  • AI Is Coming for ERP. How Will IBM i Respond?
  • The Power And Storage Price Wiggling Continues – Again
  • LaserVault Adds Multi-Path Support To ViTL
  • As I See It: Spacing Out
  • IBM i PTF Guide, Volume 27, Numbers 34, 35, And 36

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