Numeric Data Areas are a little trickier than Alphameric
Updating any numeric DTAARA in RPGLE is slightly different in RPG /FREEFORMAT than in the good old fashioned column based RPG/400. Using RPG ILE, data structures are treated as mixed-character data. The subfields making up a data structure can be a mix of all data types: The nature of a data area is that it’s a string of data that can contain a mixture of various data formats.
If we play with the local data area (*LDA) its essentially a long string of stuff:
Of course this string of data can be populated with a mixture of values:
So, what do we do to update a numeric only data area?
The answer is simple – define a data structure with one numeric field in it and use that field.
Read a simple Nine Character Numeric Data Area
Updating a Numeric Data Structure in RPG/FREE
Let’s stop my waffle and look at an example. That is why you are here after all.
I have a numeric DS called EMLRECNUM, cunningly designed to hold an increment count of the number of emails floated out into the ether.
A common error example
We can define a Data Structure like this, and it looks correct:
dcl-ds EmlRecNum dtaara('EMLRECNUM') qualified; count zoned(9:0); end-ds ;
and when I try to update it like this:
in *lock EmlRecNum; emlrecnum.count += 1; out EmlRecNum;
Most annoyingly it all compiles and looks/feels like a jolly simple peace of RPG code.
But when you run it… oh boy... big wet explosion follows:
How do we fix this?
The problem is the difference between types of numeric field. Essentially – by default numeric values are packed when stored in IBM i, and when they are unpacked, they are called zoned. So, my previous example i defined it as zoned, so RPG was completely happy, but when I run the program it reads in a packed data structure and SPLATTER
So, I like to simply define the data area as a single field which is packed. The RPG Program will handle unpacking and using the field as we touch it.
If the only thing in the DTAARA is a *DEC value, use a stand-a-lone field rather than a data structure:
dcl-s EmlRecNum packed(9:0) dtaara('EMLRECNUM');
We treat the update in the exact same way:
in *lock EmlRecNum; emlrecnum += 1; out EmlRecNum;
And thats that.
This little zoned/packed problem made me scratch my head for waaay too long. I hope it helps some other propellor headed IBM-i programmer type chaps or chappettes or transchapperoons out there 🙂
In your example you change two things. You go from DS to single variable, but you also go from zoned to packed. *DEC is packed. Zoned is stored differently on disk so zoned wouldn’t work regardless. Just curious if you tried that and it still didn’t work?
I tried by declaring a data area in the program as ZONED and it worked fine.