I have to convert a shell script from Oracle to db2. I found on this forum a sample of Oracle script I used ; it looks like this
#!/bin/bash
OUT=`$ORACLE_HOME/bin/sqlplus -s user/pass@instance << EOF
select sysdate from dual;
exit success
EOF`
echo $OUT
This will output “03-OCT-11” (Oracle sysdate). My db2 script looks like this
#!/bin/bash
db2bin="/users/db2inst1/sqllib/bin"
#connect
$db2bin/db2 connect to myschema;
#query
$db2bin/db2 "SELECT CURRENT_DATE FROM SYSIBM.SYSDUMMY1 WITH UR";
#debug
echo $?
#check
if [ $? = "0" ] then echo "found-do something"
else echo "not found-good bye"
fi
#terminate
$db2bin/db2 quit;
It works but does not retrieve the date ; only “0” or “1” (true/false). How can I retrieve the date from my Db2 query result??
Chris, your environment variables and your DB2 command path should be set by sourcing db2profile. The quit command is unnecessary when calling the db2 command with either a SQL file or a single statement specified as part of the command-line.
Your database connection will remain available until the script ends, so you can run successive statements without reconnecting.