sohan wrote:
THanks Brian.
So now I have written the following shell script to connect to a db2
database and execute a query. Is there any better way / alternate way
of doing this?
#!/usr/bin/ksh
db2 +t connect to <dbname>;
SQLSTMT=/tmp/$0.$$.tmp
echo $SQLSTMT
cat <<EOF $SQLSTMT
set schema ADMIN;
EXPORT TO x.unl OF DEL MODIFIED BY NOCHARDEL
select * from store;
EOF
db2 -txf $SQLSTMT
echo $?
Thanks,
Sohan
P.S. I am new to db2 and UNIX.
Brian Tkatch wrote:
sohan wrote:
Hi,
>
I want to know how to connect and execute a db2 query from inside a
UNIX shell script.
>
Details: We have a unix shell script. We need to execute multiple db2
sql queries from this shell script and export the result to a file.
>
Any code snippet on this will be helpful.
>
Thanks,
Sohan
db2 <command>
Most likely, you want to quote the command.
To see how to export to a file, strip headers, or the like, see the
Command Reference, and the list of options.
B.
Instead of cat to a file and running the file, the statement itself can
be used:
db2 -txf <<EOF $SQLSTMT
set schema ADMIN;
EXPORT TO x.unl OF DEL MODIFIED BY NOCHARDEL select * from store;
EOF
Of course, if only one statement is used, the SET SCHEMA is extraneous,
and the schema could be mentioned as part of the TABLE name. This would
make it one statement, and obviate the need for a here-document.
db2 -tx "EXPORT TO x.unl OF DEL MODIFIED BY NOCHARDEL select * from
ADMIN.store;"
Of course, the asterisk may then cause globbing issues, so it needs to
be escaped, or the statement needs to be quoted. Further, with one
statement, the semi-colon is redundant, making the most efficient
statement:
db2 -x EXPORT TO x.unl OF DEL MODIFIED BY NOCHARDEL select \* from
ADMIN.store
If you are looking to automate exporting and loading multiple TABLE, i
just wrote a script to do that (with some very kind help from this
newsgroup).
B.