Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / December 2007

Tip: Looking for answers? Try searching our database.

How to execute db2 query from a unix shell script

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sohan - 13 Dec 2006 05:28 GMT
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
Brian Tkatch - 13 Dec 2006 05:47 GMT
> Hi,
>
[quoted text clipped - 8 lines]
> 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.
sohan - 13 Dec 2006 08:16 GMT
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.

> > Hi,
> >
[quoted text clipped - 17 lines]
>
> B.
Knut Stolze - 13 Dec 2006 09:58 GMT
> THanks Brian.
>
[quoted text clipped - 16 lines]
> db2 -txf $SQLSTMT
> echo $?

Looks good.  You probably want to remove the temp file afterwards, though.

If your SQL statements are not too complex, you can also do this:

#!/bin/sh

db2 "CONNECT TO <dbname>"

db2 "SET SCHEMA = admin"
db2 "EXPORT TO x.unl OF DEL MODIFIED BY NOCHARDEL SELECT * FROM store"

Signature

Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

Brian Tkatch - 13 Dec 2006 14:57 GMT
> THanks Brian.
>
[quoted text clipped - 42 lines]
> >
> > 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.
teckguan - 26 Dec 2007 03:13 GMT
Hi Brian,

I am still new to db2 and not really understand the coding part.

db2 -txf <<EOF > $SQLSTMT
set schema ADMIN;
EXPORT TO x.unl OF DEL MODIFIED BY NOCHARDEL select * from store;
EOF

I have a few questions that I hope that you can help me by explaining it.
Thanks.
1. If I have several tables to be executed through Shell scripts, can I use
the same command?
2. For $SQLSTMT in the coding, I would like to ask is that the database name?

3. For << EOF > in the coding, may I know what is the meaning of it?

Thanks.

Regards,
Teck Guan.

>> THanks Brian.
>>
[quoted text clipped - 30 lines]
>
>B.
Hemant Shah - 14 Dec 2006 02:05 GMT
print -R "CONNECT TO ${DB2DBDFT};
DROP TABLE ${SCHEMANAME}.TABLE1;
DROP TABLE ${SCHEMANAME}.TABLE2;
DROP TABLE ${SCHEMANAME}.TABLE3;
TERMINATE;" | db2 +p -t

> Hi,
>
[quoted text clipped - 8 lines]
> Thanks,
> Sohan

Signature

Hemant Shah                           /"\  ASCII ribbon campaign
E-mail: NoJunkMailshah@xnet.com       \ /  ---------------------
                                      X     against HTML mail
TO REPLY, REMOVE NoJunkMail           / \      and postings      
FROM MY E-MAIL ADDRESS.          
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind,                Above opinions are mine only.
it's backed up on tape somewhere.      Others can have their own.

Knut Stolze - 14 Dec 2006 08:50 GMT
> print -R "CONNECT TO ${DB2DBDFT};

You should have pointed out that "print" is not necessarily a regular Unix
and/or shell command.

> DROP TABLE ${SCHEMANAME}.TABLE1;
> DROP TABLE ${SCHEMANAME}.TABLE2;
> DROP TABLE ${SCHEMANAME}.TABLE3;
> TERMINATE;" | db2 +p -t

Signature

Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

sohan - 14 Dec 2006 11:25 GMT
Thanks for all the responses.
One more question, to connect to a db2 database in the same UNIX server
we can use:

db2 connect to <dbname> USer <name> using <pwd>

Now if the db2 server is in differrent server where/how do we specify
the server name or its ip address?

Thanks,
Sohan

> > print -R "CONNECT TO ${DB2DBDFT};
>
[quoted text clipped - 5 lines]
> > DROP TABLE ${SCHEMANAME}.TABLE3;
> > TERMINATE;" | db2 +p -t
Ian - 14 Dec 2006 14:04 GMT
> Now if the db2 server is in differrent server where/how do we specify
> the server name or its ip address?

That is configured when you catalog the database (i.e. the DB2 client
must be installed on the machine)
Hemant Shah - 14 Dec 2006 14:21 GMT
Sorry about that, I should have mentioned that it is a Korn shell command.

>> print -R "CONNECT TO ${DB2DBDFT};
>
[quoted text clipped - 5 lines]
>> DROP TABLE ${SCHEMANAME}.TABLE3;
>> TERMINATE;" | db2 +p -t

Signature

Hemant Shah                           /"\  ASCII ribbon campaign
E-mail: NoJunkMailshah@xnet.com       \ /  ---------------------
                                      X     against HTML mail
TO REPLY, REMOVE NoJunkMail           / \      and postings      
FROM MY E-MAIL ADDRESS.          
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind,                Above opinions are mine only.
it's backed up on tape somewhere.      Others can have their own.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.