I really like the db2 command available in QShell. It's a simple shell command that runs an SQL statement it receives as a parameter. It will even return result sets from SQL SELECT or CALL statements. Not only is this tool useful from QShell, but it's also useful in CL programs, because CL doesn't have its own embedded SQL like other ILE languages do.
Unfortunately, the db2 command from QShell is missing some details--small things that seriously reduce its value as a programming tool. If it fails, it doesn't report the failure via its exit status like other QShell utilities do, and that makes error handling very difficult. It also doesn't support the system naming convention that's widely used in native applications, which means you can't use the library list. It always outputs column heading information whether you want it or not. These limitations drove me crazy, so I wrote my own utility as a replacement. My utility is intended to work exactly like the QShell tool, except that it solves these problems and adds a few new features.
Here are the things that drive me nuts about the IBM db2 QShell utility:
My version, which I named "sql," solves those problems. It's a regular ILE program, written in ILE C (I prefer to use RPG when I can, but this was a situation in which it was easier to use C.) It solves all the preceding problems, because it
This means that you can now do error handling like this:
ADDENVVAR ENVVAR(QIBM_QSH_CMD_ESCAPE_MSG) VALUE(Y) REPLACE(*YES)
STRQSH CMD('sql "select prod, desc from itmmast"')
MONMSG MSGID(QSH0000) EXEC(DO)
/* code to run if SQL statement failed in error */
ENDDO
In addition to fixing the problems I mention above, I also added several new features that I thought would be useful:
For example, with the new tool, you could do the following and it would create a CSV file:
sql -QqS -b '"' -a'"' -c '","' "select * from custmas"
The -b (before) switch prints a string before each record. Likewise, -a prints a string after each record, and -c (column) prints text between the columns. This makes it easy to generate a delimited file like the one above.
The same technique could be used to generate an HTML file:
export HTML /home/klemscot/test.html
echo '<html><body><table style="border-collapse: collapse">' > $HTML
sql -SQq -b "<tr><td>" -a "</td></tr>" -c "</td><td>"
"select improd, imdesc from itmmast" >> $HTML
echo "</body></html>" >> $HTML
Of course, if you substitute XML tags for HTML tags, you've created an XML document. Isn't that cool?
If you run this new QShell utility and pass -h only as a parameter, it will print out some help for the command to make it easier for you to get started. Here's what that help looks like:
Usage: sql -h = display help
or sql [-vqQWS] [-t | -d | -T<char>] [-r RDBNAME [-u USER] -p PASS] ]
[-b STRING] [-a STRING] [-c STRING] "statement; statement ..."
or sql [-vqQWS] [-t | -d | -T<char>] [-r RDBNAME [-u USER] -p PASS] ]
[-b STRING] [-a STRING] [-c STRING] -f FILENAME DEFAULT_LIB [LIBL]
The first syntax (with -h) displays this help file.
The second syntax runs sql statements provided on the command line. Multiple statements can be separated by a semicolon or whatever is specified with -t, -d, or -T. Statements beginning with @ will be executed as CL commands, others as SQL statements.
The third syntax runs sql statements provided in a script file. Multiple statements are separated by a newline (or whatever is specified with -t, -d, or -T). Lines beginning with -- or the statement separator are comments. Lines beginning with @ are CL commands, and others are SQL statements. FILENAME must be in IFS syntax. If FILENAME is -, statements are read from stdin. If DEFAULT_LIB is given, it will be the default library for unqualified filenames. If -Q is given, you may also specify a space-delimited library list, following the default library.
OPTIONS:
You can download the source code as well as installation instructions. [2] I hope you find it useful!
Links:
[1] http://systeminetwork.com/author/scott-klement
[2] http://www.pentontech.com/IBMContent/Documents/article/58196_969_SqlQsh.zip
[3] http://systeminetwork.com/article/qshell-does-sql
[4] http://systeminetwork.com/article/read-sql-result-sets-cl-qshell
[5] http://systeminetwork.com/article/create-physical-file-cl
[6] http://systeminetwork.com/article/write-your-own-custom-csv-export-utility-without-apis