• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Error Checking and Embedded SQL

    June 8, 2005 Hey, Ted

    We are still new to the world of embedded SQL. We are still trying to determine how to check for success or failure of SQL commands. So far we have determined that we should use the SQL code and SQL state variables, both of which seem to serve the same purpose. Can you give us some direction?

    –Pat

    Good question, Pat. I have seen programs that had no error checking for the SQL commands. When something goes wrong, they just keep on truckin’! Then people wonder why the database is messed up.

    Here’s the method I use. It has worked well for me.

    I define a return-code parameter in the program that contains embedded SQL. In my case, the program is written in RPG, so I’ll use that language for my examples.

    D MYPGM           pr                  extpgm('MYPGM')
    D  ouStatus                      8a
    D MYPGM           pi                
    D  ouStatus                      8a
    

    Here’s the “old” method to define the parameter.

    C     *entry        plist                                      
    C                   parm                    ouStatus          8
    

    When the program begins to run, I set the parameter to the value 00-00000.

    D AllOK           c                   const('00-00000')
    
    C                   eval      ouStatus = AllOK
    

    When an SQL command fails, I set the first two characters of the parameter to a two-digit number that uniquely identifies the SQL command and copy the SQL state variable to the last five characters.

    D SQLNormal       c                   const('00000') 
    D SQLEOF          c                   const('02000')
    
    C/exec sql                                              
    C+       prepare SqlStmt from :SqlCommand               
    C/end-exec                                              
    C                   if        SQLStt <> SQLNormal
    C                   eval      ouStatus = '12-' + SqlStt 
    C                   return                              
    C                   endif                               
    C/exec sql                                              
    C+    open input                                        
    C/end-exec                                              
    C                   if        SQLStt <> SQLNormal
    C                   eval      ouStatus = '14-' + SqlStt 
    C                   return                              
    C                   endif                               
    

    This method tells me where the program failed and gives me some idea of the nature of the error.

    The calling CL program tests the parameter and cancels if something went wrong.

    dcl   &Status        *char      8
    
    call mypgm parm(&Status)                                
    if (&Status *ne '00-00000') do                                
       SndPgmMsg  MsgID(CPF9898) MsgF(QCPFMSG) +                  
                    MsgDta('Program MYPGM +                      
                            ended abnormally with status' *BCAT + 
                            &Status) MsgType(*ESCAPE)                     
    enddo
    

    A couple more notes and I’m done. First, whether you use SQL state or SQL code is a matter of preference. Everything I read tells me that the SQL state mechanism is the more standard of the two, so that’s what I use.


    Second, you’ll have to determine which values indicate success or failure. For example, SQL state 02000 indicates end-of-file for a fetch, and that is fine when you’re reading a result set through a cursor. However, there may be circumstances when you issue only one fetch and want to make sure that the fetch retrieved data. In that case, 02000 is not acceptable.

    The same thing can be said of warnings, which are SQL state values greater than 00000 and less than 02000. In some cases, you may want to ignore some or all warnings, while in other cases, you may want to treat warnings as errors.

    –Ted

    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

    Raz-Lee Ships New iSeries Security Software GST Speeds Backups with New Virtual Tape Library

    Leave a Reply Cancel reply

Volume 5, Number 22 -- June 8, 2005
THIS ISSUE
SPONSORED BY:

WorksRight Software
Advanced Systems Concepts
Guild Companies

Table of Contents

  • Error Checking and Embedded SQL
  • Query/400 Does Exponentiation, Sort Of
  • Admin Alert: A Better Technique for Detecting Invalid Log-In Attempts

Content archive

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

Recent Posts

  • To Comfort The Afflicted And Afflict The Comfortable
  • How FalconStor Is Reinventing Itself, And Why IBM Noticed
  • Guru: When Procedure Driven RPG Really Works
  • Vendors Fill In The Gaps With IBM’s New MFA Solution
  • IBM i PTF Guide, Volume 27, Number 27
  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26

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