Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: tliottaPosted on 2005-03-17 at 18:32:14ID: 13571340
Don:
Yes, yes and no.
That is, CL and SQL don't exactly go together; but there are a couple ways to get it to work.
Easiest is via the RUNSQLSTM command. It will execute an SQL statement or a series of statements delimited by semi-colons (";"). The troubles are that (1) the statement must be in a source file member and (2) only statements that can executed immediately are allowed.
There's a more technical explanation of that second part, but I don't have a decent link for it off the top of my head. One critical result is that there's no good way of using a SELECT statement directly via RUNSQLSTM such as you might in interactive SQL, i.e., the STRSQL command.
After RUNSQLSTM, there is STRQMQRY. Now, this also needs a source member to hold the SQL statement and it further needs to be compiled into a *QMQRY object.
But it has a very useful saving grace -- the source is allowed to have a whole bunch of "substitution variables" that get substituted into the 'statement' at run-time. While that only seems useful in general, it becomes seriously useful when you discover that _any_ part of the statement can be replaced by a substitution value.
Consider first:
SELECT &a FROM &b
That means you can choose both your column list and your table list at run-time. Those values would be passed in as parameters to the STRQMQRY command. Not bad.
But now consider this:
&a&b&c&d&e&f
The _entire statement_ is nothing but a series of substitution variables. That is, you can pass in a series of parameter values that string together to create the actual SQL statement on the fly.
There are a couple odd tricks. First, a substitution _value_ cannot be more than 55 characters in length. Second, the substitution variable names are case-sensitive -- when you pass the variable name into STRQMQRY along with the value, you have to pass the name in the proper case. Finally, if you want an SQL statement that will contain quotes, e.g.:
SELECT a,b,c FROM mylib/myfile WHERE a='A1B2'
...then you need to be careful in how you construct the substitution values for the STRQMQRY command parameters.
Fortunately, most of this has already been done for you. You can run internet searches for SQL and CL and find examples that you can download. I have an odd example at my web site though it's primarily for demonstration because the processing is REXX.
You can look it over at < http://zap.to/tl400 >. Look in the Files section under REXX RunSQL. It provides a RUNSQL command that essentially lets you pass an SQL statement in as a parameter. It breaks the string into 55 character values and passes them into a STRQMQRY command. It also provides for output types of *OUTFILE and *PRINT as well as to the display.
For many displays, I most often use just:
RUNQRY *n mylib/myfile
It's on all AS/400s. If you try to prompt it and use some of the advanced parameters, you'll need Query/400 installed.
Also note that CL can make use of OPNQRYF along with RCVF to process database records.
Tom