VALUES is an extremely useful shortcut in everything from embedded SQL to your favorite SQL client.
SQL provides a wide variety of functions that can be extremely useful to programmers, tasks that might otherwise take many lines of RPG code. ILE RPG has gotten much better at providing some of these base functions and also at making it easy for programmers to write their own function libraries and make them available as service programs, but I always prefer to take advantage of built-in capabilities rather than reinvent the wheel. In this article, I'll show you an easy way to use some of the capabilities of DB2 to do magic in your programs. UCASE, LCASE, MIN, MAX, and other SQL functions provide access to basic programming techniques that simply aren't as easy to do in RPG. The question is how to make those functions available to standard RPG code; the VALUES statement is the key to unlocking that power.
SQL Is Great, but It's Not Always Easy to Use
In the past, trying to get something into an RPG program from SQL usually involved a cursor and a FETCH; it just wasn't very convenient. IBM even had to set up a special file, SYSDUMMY1 in library SYSIBM, that had only one record. You could use it in a select statement in order to get a single value:
SELECT UCASE(:MYVAR) FROM SYSIBM/SYSDUMMY1
This was the way to convert MYVAR to uppercase. However, you still needed a cursor with that select statement, an open, a fetch, and a close:
DECLARE C1 CURSOR FOR SELECT UCASE(:MYVAR) FROM SYSIBM/SYSDUMMY1
OPEN C1
FETCH FROM C1 INTO :MYVARU
CLOSE C1
This was even uglier back in the pre-freeform RPG days, when each of those needed to be inside of an EXEC SQL and END-EXEC pair. At least now you can simply prefix each line with EXEC SQL. But that's still a lot of work in order to be able to take advantage of a simple SQL scalar function. In fact, there's really no reason not to just roll your own conversion using the old-fashioned technique of defining your own upper- and lowercase alphabet variables and then using the %XLATE function:
dcl-c UP 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
dcl-c LO 'abcdefghijklmnopqrstuvwxyz';
MVARU = %xlate(LO:UP:MYVAR);
VALUES to the Rescue!
What would make it easier? The answer is a relatively unknown but phenomenally flexible capability, the VALUES statement. VALUES is really the SQL shorthand for "evaluate this and return it." The key to that statement is the phrase "return it" because VALUES is eminently flexible in what it returns. Let's start with our situation above, where we want to take advantage of the UCASE scalar function. Here's the way we do it in freeform ILE RPG using VALUES:
EXEC SQL VALUES UCASE(:MYVAR) INTO :MYVARU;
Boom! That's it! It only takes a single line of code to call an SQL function to convert MYVAR to uppercase. At this point, it's even easier than the XLATE; no need to define your own alphabet variables! One minor caveat: I haven't done any timing analysis, and I suspect the call to SQL might be a bit slower than the equivalent XLATE function, but at the same time I would guess that the UCASE is probably smarter, especially about things like language differences. So the tradeoff is probably OK unless you're processing millions of transactions in a batch process.
And of course that's just one SQL function. DB2 has dozens of scalar functions available that provide capabilities not readily available in ILE RPG. One of my favorites is MAX. MAX returns the maximum value of an arbitrary list of variables, and MIN returns the minimum. How many times have you needed to compute a value but make sure it doesn't go over a limit?
EXEC SQL VALUES MIN( (COST*QTY), 9999999.99) INTO :LINECOST;
LINECOST will now contain the value of COST time QTY not to exceed 9999999.99 total dollars. This is especially important nowadays when you're dealing with ILE RPG and the fact that overflowing a variable causes a halt in the program. You can see where this might come in handy. I've had to write routines where I pass a value into a function that checks its range before updating another variable; the generic version of that function takes a little doing. And there are many other scalar functions available to do everything from string handling to advanced mathematics (ACOS, anyone?) to advanced XML handling.
VALUES Isn't Limited to Scalars
You could probably argue that, for most scalar functions, you could write a comparable ILE RPG routine and make it available via a service program. That's true, although, as I've noted, making those routines generic takes a little careful planning. Still, it's not out of the range of most of us, especially when we have the C libraries available for so many functions (especially the higher math). But using VALUES for scalars is only scratching the surface of this feature. VALUES is just as at home computing aggregate values, and that can come in very handy. Want to know the highest order number?
EXEC SQL VALUES (SELECT MAX(OHORD) FROM ORDHDR) INTO :MAXORDER;
That's it. There's your highest order number. Want to know the number of orders for a customer?
EXEC SQL VALUES (SELECT COUNT(*) FROM ORDHDR WHERE OHCUST = :MYCUST) INTO :NUMORDERS;
Pretty much anything you can SELECT using an SQL statement you can get into a variable just by using the equivalent VALUES statement. It's that simple. The beauty, I think, is that the SQLRPGLE preprocessor makes it so easy to integrate variables from your program directly into the SQL statements; it's as if SQL syntax is just an extension of RPG now. I'm currently trying to get versed in the XML functions; DB2 includes a wonderfully powerful XSLTRANSLATE function that can convert XML to other formats, which can be a quick way to create PDF documents in RPG. I'll write about that in a future article. Version 7.2 added regular expression processing as well.
DB2 also has a number of functions that make it uniquely suited for the IBM ecosystem—for example, the various MQ functions. Using these, you can send and receive messages directly to an MQSeries queue, which can in turn easily send that data to other systems in your network. It's a quick and powerful way to provide MQSeries connectivity to your RPG programs (and a lot easier than calling the MQSeries APIs yourself!). With the VALUES clause, you can simply do this to talk to another machine:
EXEC SQL VALUES (MQREAD('MYSERVICE')) INTO :MESSAGE;
It doesn't get a lot simpler than that!
So acquaint yourself with the VALUES statement and learn to use it in your RPG programs. As time goes on, I'll give you more examples of how to use DB2's functions to enhance your RPG programs.
LATEST COMMENTS
MC Press Online