Programming Standards – Variables and Constants in SQLRPGLE

  • Home
  • /
  • Blog
  • /
  • Programming Standards – Variables and Constants in SQLRPGLE

June 2, 2015

Over the last year I’ve been using SQL more and more for all my RPG file access needs. The more I use it the more I like it and I’m building my own little set of reusable components and settled in with some naming standard that work for me.

I thought I would enter them here in case they helped someone else, or one of the many freshers out there learning to program with RPG on the IBM i System.

Never ever start variables or constant names with ‘SQL’something, those names are reserved for the SQL Precompiler (for example for defining fields within the SQLCA or SQLDA).

Even though there is today no field named SQLYourVar, IBM may update the data structures tomorrow and integrate a new subfields with exactly the name of your variable or constant and them *boom* your program stops working: The “best” thing that can happen is that your program will no longer compile. If the compile completes normally it is even worse, because wrong information is moved to your parameters or the SQL variables.


Use CONST for all inputs and then generate a return variable. In the olden days of RPG3 and RPG400 – a parameter was typically passed into a routine, updated, and returned. Scrap this technique and lean towards passing in a variable as a CONST and then returning a fresh variable as the return version of that. ie: ADDRESS CONST is passed in and RtnAddress Variable is returned. It just makes for cleaner code.

$ for variables?

Some people hate prefixing field names with special characters. I’ve been editing HTML and PHP for a while and like the standard of prefixing a variable with a “$” to show its a variable. So if I see a field called “$count” or “$something” then I know its a transient variable, a throw away variable, something that is just used for this procedure

sub procedures?

Use sub-procedures for any small “single use” pieces of code. Make sure that parameters passed in are either pointers (an efficient way of pointing at a memory space) or just code then as varying values.

d s 1024a varying
d s 1024a varying

Checking SQL status?

dcl-c SquirrelSuccess const('00000')
dcl-c SquirrelNoData const('02000')
dcl-c SquirrelFileClosed const('24501')

These are constant values so we can check an SQL Input/Output status after its been attempted. For example:

run sql select * from something

If SquirrelSuccess;
 // do something
elseif SquirrelNoData;
 // error handling for nothing was read
elseif SquirrelFileClosed
 // error handling for file closed

Obviously – call the variables anything you like.

No Squirrels were harmed during the making of this motion picture  😉


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Start your
7-day free trial

Take This Course with ALL ACCESS

Unlock your Learning Potential with instant access to every course and all new courses as they are released.
 [ For Serious Software Developers only ]

IBM i Training For Technology Experts


Successfully Work Remote 

 $ 129.00  $59.00

By adding new skills in information technology, employers will be confident that you have the necessary skills and tools needed to successfully work remotely


Snug CBD

 20% Discount

I have partnered with SNUG CBD givING you Organic CBD
20% discount code "NL20"