fhg
Volume 8, Number 37 -- October 29, 2008

Meaningful Names for Null Indicators

Published: October 29, 2008

by Ted Holt

There are two ways that a column of a SQL result set can contain the null value. One, it can be derived from an expression that contains a null value. Two, null is returned in outer joins and exception joins when a row in a primary table has no match in a secondary table. In either event, RPG has no way to represent a null value, so what's a guy (a professional i-on-Power guy, that is) to do when an embedded SQL command might return null values?

Since RPG variables cannot be null, SQL loads two-byte (five-digit) integer variables with zeros and negative ones. Zeros indicate non-null values. Negative ones indicate null values. These integers are properly called null indicators.

To indicate that a null indicator corresponds to a data variable, follow the data variable with the null indicator in the FETCH command. Do not separate the two with a comma.

exec sql
   fetch TheCursor into :Name :NameNullInd;

Notice that both the data variable and the null indicator are prefixed with colons, as they both host variables.

If the data variable is a data structure, use an array for the indicator variable. You can test individual array elements in order to determine if a column returned null or not. In the following example, the programmer tests element four of array NullsInds in order to determine whether the item description came back null or not.

D InvoiceData     ds                                        
D  InvNbr                        5p 0                       
D  InvLineNbr                    3p 0                       
D  ItemNbr                       6a                         
D  ItemDescr                    30a                         
D  ItemPrice                     5p 2                       
D  QtySold                       3p 0                       
                                                            
D NullInds        s              5i 0 dim(6)                
                                                            
D NullValue       s                   like(NullInds) inz(-1)
                                                            
 /free                                                      
     exec sql                                               
        declare Invoice cursor for                          
           SELECT Ln.InvNbr, Ln.LineNbr,                    
                  Ln.ItemNbr, Itm.Descrip,                  
                  Ln.Price, Ln.Qty                          
             FROM InvLine as Ln                             
             LEFT JOIN Items as Itm                         
               ON Ln.ItemNbr = Itm.Item;                    
                                                     
     *inlr = *on;                                    
                                                     
     exec sql                                        
        open Invoice;                                
                                                     
     dow '1';                                        
        exec sql                                     
           fetch Invoice into :InvoiceData :NullInds;
        if sqlstt = '02000';                         
           leave;                                    
        endif;                                       

        if NullInds(4) = NullValue;                  
           // do whatever                            
        endif;  
                                   
     enddo;                                          
                                                     
     exec sql                                        
        close invoice;                               
     return;            

For a short example, cryptic code is no big deal. In a larger program, you might find it advantageous to give the null indicator a name. You can do so by overlaying the array with a data structure. Here's one way to accomplish that.

D InvoiceData     ds                                        
D  InvNbr                        5p 0                       
D  InvLineNbr                    3p 0                       
D  ItemNbr                       6a                         
D  ItemDescr                    30a                         
D  ItemPrice                     5p 2                       
D  QtySold                       3p 0                       
                                                            
D NullInds        s              5i 0 dim(6)                
D NullAddr        s               *   inz(%addr(NullInds))  
                                                            
D InvoiceNulls    ds                  based(NullAddr)       
D  NullInvNbr                         like(NullInds)        
D  NullLineNbr                        like(NullInds)        
D  NullItemNbr                        like(NullInds)        

D  NullItemDescr                      like(NullInds)        

D  NullItemPrice                      like(NullInds)        
D  NullQtySold                        like(NullInds)        
                                                            
D NullValue       s                   like(NullInds) inz(-1)
                                                            
 /free                                               
     exec sql                                        
        declare Invoice cursor for                   
           SELECT Ln.InvNbr, Ln.LineNbr,             
                  Ln.ItemNbr, Itm.Descrip,           
                  Ln.Price, Ln.Qty                   
             FROM InvLine as Ln                      
             LEFT JOIN Items as Itm                  
               ON Ln.ItemNbr = Itm.Item;             
                                                     
     *inlr = *on;                                    
                                                     
     exec sql                                        
        open Invoice;                                
                                                     
     dow '1';                                        
        exec sql                                     
           fetch Invoice into :InvoiceData :NullInds;
        if sqlstt = '02000';                         
           leave;                                    
        endif;                                       

        if NullItemDescr = NullValue;                
           // do whatever                            
        endif;          

     enddo;             
                        
     exec sql           
        close invoice;  
     return;            

In this example, NullItemDesc is another name for the fourth element of NullInds.




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
ALDON


Modernize Don't Replace IBM i Applications.

Billions of lines of code have been written for crucial business operations
from finance to customer relations – all on the IBM i platform.

Find out how to modernize them with new graphical user interfaces,
high availability, web enablement and more by downloading
a free whitepaper from Aldon –
Modernizing IBM i Applications for Multi-platform development.

Download Here



Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Quadrant Software:  Going paperless pays off! Watch our exclusive Webinar series
COMMON:  Join us at the 2009 annual meeting and expo, April 26-30, Reno, Nevada
Vision Solutions:  A $20 gas card for completing a short i5/OS DR survey


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
Getting Started with PHP for i5/OS: List Price, $59.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
IBM's Q3 in Servers, Redux: The i and p Platforms Do OK

So Much For That Unbundled i Compiler Tool Pricing

SOA Without the Middleware, Without the Pressure

As I See It: The IT Election

Gartner Outlines the Key IT for 2009

The Linux Beacon
Why Blade Servers Still Don't Cut It, and How They Might

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Microsoft Ponies Up Another $100 Million for Novell Linux

Mad Dog 21/21: Newtonian Economics

Two More Xeon-Based Galaxy Servers from Sun

Four Hundred Stuff
Vision Integrates Clustering Tool into IBM's New Web Console

Databorough Teams with Genuitec to Push Alternative Eclipse IDE

Computer Retailer CDW Taps Linoma for Database Encryption

Lawson Delivers Demand Planning, BI Tools to M3 Customers

Shop-Vac Ditches IBM, Taps CYBRA for Native i OS RFID Software

Big Iron
For Some Customers, the Mainframe Is Green

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
October 25, 2008: Volume 10, Number 43

October 18, 2008: Volume 10, Number 42

October 11, 2008: Volume 10, Number 41

October 4, 2008: Volume 10, Number 40

September 27, 2008: Volume 10, Number 39

September 20, 2008: Volume 10, Number 38

The Windows Observer
Citrix Addresses Performance with XenApp 5

Server Buyers Shop Like It's 1999 in the Second Quarter

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Mad Dog 21/21: Newtonian Economics

Microsoft Does Something About Those SQL Injection Attacks

The Unix Guardian
What the Heck Is the Midrange, Anyway?

Overseas and Notebook Sales Offset Printer Declines for HP in Q3

Two More Xeon-Based Galaxy Servers from Sun

Mad Dog 21/21: Newtonian Economics

Intel's Nehalems to Star at IDF, AMD Pitches Shanghai

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

ProData Computer Services
Aldon
WorksRight Software


Printer Friendly Version


TABLE OF CONTENTS
Calculate a Fractional Number of Years Difference Between Two Dates in SQL

Meaningful Names for Null Indicators

Pimp My Font Size, Win a No-Prize

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Data Queues vs. MQ Series: Performance

Removing blanks from a CL Variable

XML

SQL "Hidden" Field

Java Messages

MQ Help Desired





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement