By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,514 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,514 IT Pros & Developers. It's quick & easy.

DB2 + shell script

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.