Is there a better way to do this or does the AS400 lack the required flexibility?
That’s a tricky question. There are many possibly alternatives between “better way” and “lack the required flexibility”.
For dynamic database access, you need to code dynamic access routines. In general, that’s done the same way on AS/400s as it’s done elsewhere. The result (as always) is some pretty difficult code.
Four potential ways you might go:
- The Call Level Interface (CLI) APIs allow you do just about anything that you could do with ODBC. This includes dynamic SQL statements and dynamic parameters. If you’ve done ODBC programming, most of those should be quickly familiar.
- The Extended Dynamic Remote SQL (EDRS) APIs (probably with the Process Extended Dynamic SQL (QSQPRCED) API) let you do some fancier things, but few need more than ODBC.
- REXX is always available for dynamic SQL processing. It’s fundamentally dynamic anyway. Creating variables on the fly is standard stuff for REXX. The SQL is free.
- The ILE C run-time library functions (PDF) include just about anything you’d ever need. The functions can be called from any ILE language, including the memory allocation/deallocation and other functions that you’re going to use for your dynamic work.
And there are all of the oldies-but-goodies that can be built around a generic program-described file plus OVRDBF LVLCHK(*NO), possibly combined with a dynamically generated OPNQRYF. And Query Manager queries can be dynamically constructed as well. And… there are other possibilities.
Many of those put you out into risky territory. You necessarily bypass some safeguards that the integrated nature of DB2, i5/OS and the language compilers provides to developers.
But it’s certainly there if you want it.