Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 14th, 2006, 09:55 AM
bikkaran@in.ibm.com
Guest
 
Posts: n/a
Default 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

  #2  
Old March 14th, 2006, 01:45 PM
Michel Esber
Guest
 
Posts: n/a
Default Re: DB2 + shell script

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





bikkaran@in.ibm.com escreveu:
[color=blue]
> 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[/color]

  #3  
Old March 14th, 2006, 02:15 PM
Eugene F
Guest
 
Posts: n/a
Default Re: DB2 + shell script

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

  #4  
Old March 14th, 2006, 03:25 PM
Dave Hughes
Guest
 
Posts: n/a
Default Re: DB2 + shell script

Eugene F wrote:
[color=blue]
> 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[/color]

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.
--

  #5  
Old March 20th, 2006, 12:15 PM
bikkaran@in.ibm.com
Guest
 
Posts: n/a
Default Re: DB2 + shell script

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

  #6  
Old March 20th, 2006, 02:25 PM
Brian Tkatch
Guest
 
Posts: n/a
Default Re: DB2 + shell script

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.

  #7  
Old April 3rd, 2006, 11:25 AM
Anurag
Guest
 
Posts: n/a
Default Re: DB2 + shell script

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

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles