• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Handling Constraints Revisited

    March 31, 2015 Paul Tuohy

    I recently had an email from Doug, who was making use of some code I had published way back in February 2010 in my article Handling Constraint Violations in RPG. The article discussed how to trap a constraint violation caused by a WRITE/UPDATE/DELETE operation and, more importantly, how to determine the name of the constraint that caused the violation.

    Doug had come up with an instance where my code was not working! After going through the usual stages of denial, anger, and some expensive therapy, I read the rest of the email. Doug’s code worked fine with an RPG operation code, but if he used an embedded SQL statement, he could not determine the name of the constraint.

    So what was going on?

    How It Worked

    The basic premise of the original article was that when an RPG program received a constraint violation, it would call a procedure that would search back through the program message queue of the caller and find the relevant message that contained the name of the constraint that had caused the violation.

    And therein lies the problem.

    Even though we code our SQL directly in our programs, it is not our programs that run the SQL statements. Assuming we coded the following in an RPG program:

         charIs = '6';
         exec SQL
            insert into violate
               values(:charIs);
    

    The SQL pre-compiler translates it to the following:

         charIs = '6';
      //*   exec SQL
      //*      insert into violate
      //*         values(:charIs);
     /END-FREE
     C                   EVAL      SQL_00005    = CHARIS
     C                   Z-ADD     -4            SQLER6
     C                   CALL      SQLROUTE
     C                   PARM                    SQLCA
     C                   PARM                    SQL_00000
      /FREE
    

    So, the insert is performed by the program QSYS/QSQROUTE or some program or procedure called by QSYS/QSQROUTE (in the code above, SQLROUTE is a named constant for QSYS/QSQROUTE).

    At this point, calling a subprocedure to look back through the program message queue of our program is pointless since it is the program message queue of QSYS/QSQROUTE (or some other program or procedure) that contains the appropriate message.

    The Solution

    Determining the name of the constraint that caused a constraint violation for an embedded SQL statement is a lot easier then what we had to go through in RPG. SQL simply tells us the name of the constraint.

    In the SQL Communications area (the data structure SQLCA, placed in our program by the SQL pre-compiler), the field SQLERRMC will contain the name of the constraint (if there is a constraint violation).

    There is one small caveat. SQLERRMC is defined as a 70-character field, but the name of the constraint is a varying length field. In other words, the first two characters of SQLERRMC contain the length of the constraint name. So you may want to consider defining the following data structure:

    d getName         ds            70
    d  constraint                   68a   varying
    

    And then, by copying SQLERRMC to getName, you will have the name of the constraint.

    In the example above, if I had coded:

         charIs = '6';
         exec SQL
            insert into violate
               values(:charIs);
         if (SQLSTATE = '23513');
            getName = SQLERRMC;
         endIf;
    

    Then the field constraint would have a value of BAD_STATUS_CODE_ERR0092.

    Remember to get the name directly after the SQL statement that caused the error since the contents of the SQL communication areas are reset every time an SQL statement is executed.

    There is one tidbit from the original article that I would like to repeat. When I name a constraint, the last seven characters of the name are a message ID. For instance, BAD_STATUS_CODE_ERR0092. This provides a very simple means of associating a meaningful error message with a constraint.

    Thanks Doug for bringing this to my attention. And my therapist thanks you, too!

    RELATED STORIES

    Guru: Handling Constraint Violations In RPG

    Handling Constraint Violations in RPG

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

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

    Sponsored by
    New Generation Software

    Put your IBM i Data to Work

    NGS-IQ makes it easy to securely access, format, analyze, and share critical business data across your organization.

    Join us for a look at the top challenges companies attending COMMON NAViGATE at TechXchange 2026 shared when it comes to getting reliable reports from their DB2 data — and how NGS-IQ can help provide the answers you need without breaking the budget.

    Register for the Webinar

    www.ngsi.com – (800)824-1220

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Sponsored Links

    LaserVault:  Webinar - IBM i: Intro to Tapeless Backups. April 9 Chrono-Logic:  Deploy automatically to multiple IBM i and Windows servers with a single click!! COMMON:  2015 Annual Meeting & Expo, April 26 - 29, at the Disneyland® Resort in Anaheim, California

    Reader Feedback On What’s Up In The IBM i Marketplace . . . Don’t Miss the IBM i Marketplace Webcast . . . COMMON Is Coming, Linux and VIOS Get Top Billing Dynamic Lists In Static SQL Queries

    Leave a ReplyCancel reply

Volume 15, Number 06 -- March 31, 2015
THIS ISSUE SPONSORED BY:

WorksRight Software
HelpSystems
LaserVault

Table of Contents

  • Handling Constraints Revisited
  • Dynamic Lists In Static SQL Queries
  • RubyGems Are The Foundation Of Success

Content archive

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

Recent Posts

  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38
  • Critical PHP2Shell Security Vulns Evoke ‘Black October’
  • Why Fortra Partnered with FalconStor to Launch Continuity Care: A Validated Recovery Service for IBM i
  • Guru: Analyzing User Session Statistics, Part 2
  • Fresche Launches OpsIQ, a Free AI-Based Operation Assistant for IBM i
  • IBM i PTF Guide, Volume 28, Number 37

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