473,385 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DB2 + shell script

Hi .

I have to write a Shell script like ...
IF "select value from tablnema where condition;" = some_value
then
depending upon the this value I will call some other script.sql

This query will always return 1 value.

Can some one help me how to implement this in Shell script.
regards
bikash

Mar 14 '06 #1
6 25961
Try something like this ...

#!/bin/bash
.. /home/db2inst1/sqllib/db2profile

db2 connect to <DB>

var=`db2 -x "select value from tablnema where condition"`
if [ $var -eq 0 ]
then
something
else
something
fi

bi******@in.ibm.com escreveu:
Hi .

I have to write a Shell script like ...
IF "select value from tablnema where condition;" = some_value
then
depending upon the this value I will call some other script.sql

This query will always return 1 value.

Can some one help me how to implement this in Shell script.
regards
bikash


Mar 14 '06 #2
A regular OS shell has no facility to communicate with database except
for the return code from DB2 CLP that only flags success of its
execution (0) or failure (not 0). For your task perl would be a better
choice.

-Eugene

Mar 14 '06 #3
Eugene F wrote:
A regular OS shell has no facility to communicate with database except
for the return code from DB2 CLP that only flags success of its
execution (0) or failure (not 0). For your task perl would be a better
choice.

-Eugene


Or Python or Ruby :-)

(though, as I understand it, Perl is the only one with an "official"
feature-replete driver from IBM; Ruby and Python basically have "bare
essentials" community efforts which, frankly, need quite a bit more
work)

One other quick point: if you do go the shell script route, the failure
exit codes from the CLP aren't quite standard:

0=success
1=a fetch returned no rows (not an error)
2=statement warning (not an error)
4=statement error
8=CLP error

Check the Reference / Commands / DB2 Universal Database / Command Line
Processor (CLP) / CLP return codes section in the Info Center for more
information. So, you'll want to do something like:

#!/bin/bash

db2 -x CONNECT TO $db >/dev/null
if [[ $? -ge 4 ]]; then
echo "Failed to connect to $db"
exit 1
fi
value=$(db2 -x SELECT somefield FROM sometable)
if [[ $? -ge 4 ]]; then
echo "Failed to run query"
exit 1
fi
HTH,

Dave.
--

Mar 14 '06 #4
hi ,

I want to use the value i.e. output as give by the SQL query "select
value from tablnema where condition" .

The value as above "var" will be either 0 for successful execution of
the query .......... It wont have the value returned by the query . I
want to use the out put of the query ..the result the database gave.

Thanks :)
bikash

Mar 20 '06 #5
Name the file to be executed values.sql.

Then use 'eval' to execute the output of a db2 -x (clean output).

For example, i have a script that SELECTs other CALLs from TABLE.

#!/bin/bash

db2 +o "CONNECT TO [alias] USER [user] USING [password]"
db2 +o "SET SCHEMA [schema]"
db2 +o "SET PATH [schema], CURRENT PATH"
eval "$(db2 -x "SELECT 'db2 +o \"CALL Test_Timings_Call(' || CHAR(Id)
|| ')\" && db2 +o \"UPDATE Timings_Log SET Stop = CURRENT TIMESTAMP
WHERE Stop IS NULL\"' FROM Timings_Call")"
db2 +o "CONNECT RESET"
db2 +o "TERMINATE"

It may be confusing because i am using db2 to call db2. In your case,
perhaps it may be:

#!/bin/bash

db2 +o "CONNECT TO [alias] USER [user] USING [password]"
db2 +o "SET SCHEMA [schema]"
db2 +o "SET PATH [schema], CURRENT PATH"
eval "$(db2 -x "SELECT './' || value || '.sql' WHERE condition")"
db2 +o "CONNECT RESET"
db2 +o "TERMINATE"

B.

Mar 20 '06 #6
Hey Bikash,
This one is very simple.

Sample script (and very basic) goes like this:
set stmt="select value from tablnema where condition"
db2 -x "$stmt" | read output_result

if [ $output_result -eq 1254 ]
then
..........
..........
end

This method would be troublesome if db2 -x "$stmt" returns an error.
How I handle the error situation depends on whether the expected result
is numeric or not.
If I am expecting a numeric output I just check if "$output_result" is
numeric.
If expected result is VARCHAR, I redirect the output of db2 $stmt (note
the missing "-x") to a file and check for success execution of the db2
statement and then parse the output result.

If your problem still persists, post your actual code here and I can
finetune it.

HTH..........Anurag

Apr 3 '06 #7

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

Similar topics

3
by: FPGA05 | last post by:
Hello All, I am developing a small application in which I would need a C++ application to read the output from a shell script. A shell script keeps looking for user inputs and once the user...
1
by: news | last post by:
At the end of a PHP script, I'm sending a file via FTP to a server. I thought it'd be best to use a shell script in order to automate the FTP (logging in, changing to binary, putting the file,...
0
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...
9
by: sohan | last post by:
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...
3
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I...
2
by: ellennolan | last post by:
Hello, I wonder if anyone can help with calling external shell script in c++. Basically, in the shellscript, I want to pass src, dst, md5. If the src's md5 matches md5 given, it will be link to...
2
by: smitanaik | last post by:
i have a java file which i am running through shell script. the syntax that i have used is #! /bin/bash javac Copy.java
5
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
4
by: devi thapa | last post by:
Hi, I am executing a python script in a shell script. The python script actually returns a value. So, can I get the return value in a shell script? If yes, then help me out. Regards, Devi
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.