473,703 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to execute db2 query from a unix shell script

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

Dec 13 '06 #1
9 78341
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.

Dec 13 '06 #2
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.
Dec 13 '06 #3
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 $?
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"

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Dec 13 '06 #4
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.

Dec 13 '06 #5


print -R "CONNECT TO ${DB2DBDFT};
DROP TABLE ${SCHEMANAME}.T ABLE1;
DROP TABLE ${SCHEMANAME}.T ABLE2;
DROP TABLE ${SCHEMANAME}.T ABLE3;
TERMINATE;" | db2 +p -t

While stranded on information super highway 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
--
Hemant Shah /"\ ASCII ribbon campaign
E-mail: No************@ 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.
Dec 14 '06 #6
Hemant Shah wrote:
>

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}.T ABLE1;
DROP TABLE ${SCHEMANAME}.T ABLE2;
DROP TABLE ${SCHEMANAME}.T ABLE3;
TERMINATE;" | db2 +p -t
--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Dec 14 '06 #7
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 <dbnameUSer <nameusing <pwd>

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

Thanks,
Sohan
Knut Stolze wrote:
Hemant Shah wrote:


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}.T ABLE1;
DROP TABLE ${SCHEMANAME}.T ABLE2;
DROP TABLE ${SCHEMANAME}.T ABLE3;
TERMINATE;" | db2 +p -t

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Dec 14 '06 #8
Ian
sohan wrote:
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)

Dec 14 '06 #9

Sorry about that, I should have mentioned that it is a Korn shell command.

While stranded on information super highway Knut Stolze wrote:
Hemant Shah wrote:
>>

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}.T ABLE1;
DROP TABLE ${SCHEMANAME}.T ABLE2;
DROP TABLE ${SCHEMANAME}.T ABLE3;
TERMINATE;" | db2 +p -t

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
--
Hemant Shah /"\ ASCII ribbon campaign
E-mail: No************@ 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.
Dec 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11223
by: nidhikhandelwal | last post by:
I am calling a unix shell script from a perl script by using the following system command: system("./readfile_new.sh"); but it seems the unix script readfile_new.sh is not being called. Can someone let me know how to call this script in the perl file/
0
3054
by: Aashif | last post by:
I want to call Unix Shell script which is available in other Server (Unix server) from windows application using C#. Currently the shell script runs the C program but the GUI is not good, So I want to create GUI in C# windows application and call that C program using Shell script so first I have to call unix shell script from C#. Please guide me friends.
0
2106
by: Purple Haze | last post by:
I have a stored procedure which returns a resultset. I am calling that stored procedure from UNIX shell script. I want the data in the result set to be exported to a .csv file. How can I do this using the shell script
0
1590
by: INCANDESCENT | last post by:
Hi All , Could any one of you tell me , if we can call a Unix shell script from VB forms ?? VB is used for client interaction , i have developed program using unix script which internally calls the stored procedures and functions .. i want to trigger the same script on a hit of a command button from VB . Please let me know how this could be done .. Best Regards , Incandescent .
2
10291
by: gagandutta01 | last post by:
Hi, Can anyone tell me how to execute a function declared in Oracle Package from Unix shell script? I created a shell script and after connecting to oracle database i am using exec @ Db_name.Packagename.function_name('p1','P2'); and after executing the shell scripts i am getting the follwing error:-ERROR at line 13: ORA-06550: line 13, column 2: PLS-00221: 'function_name' is not a procedure or is undefined
0
1510
by: abhishek | last post by:
any way by which i can trigger a unix shell script from a button click.after that i should display an error message if the script was'nt executed successfully.Please help!!!
3
4436
by: regnumber | last post by:
Hi. I am new to Unix. I need to write a Unix Shell Script to extract records from the table and write those extracted datas to a text file. DB using is DB2. Can anyone give me some sample shell script thanks
3
5310
by: lakk | last post by:
Having a database created, is it possible to excute queries on this database from a shell script file??
1
2744
by: Koteswara Rao K | last post by:
Hi, How to get server file( UNIX Server) to local desktop(Windows desktop) Using unix shell script Program. Thanks & Regards Koteswara Rao Kavuri
0
8756
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8667
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4429
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.