#!/usr/bin/env sh
DBNODE='nodename'
DBUSER='username'
DBPASSWORD='password'
connDB2()
{
if( db2 connect to $1 user $2 using $3 > /dev/null )
then
echo 'OK'
else
echo "failed connect to ${DBNODE}"
exit -1
fi
}
releaseDB2(){
db2 connect reset > /dev/null
}
run(){
connDB2 ${DBNODE} ${DBUSER} ${DBPASSWORD}
sql="
select
area_id ,areaname
from
pt.area
"
db2 -x ${sql}| while read area_id areaname
do
echo "Result:${area_id}->${areaname}"
done
releaseDB2
}
echo "execute sql ................."
run
如果有必要可以通过执行完SQL后$?的结果来判断是不是执行成功
if [ "$?" -ne "0" ] then echo "execute error" exit -1 fi
下面是DB2文档中找到的返回值的含义
The return code can be one of the following:
Code Description
0 DB2 command or SQL statement executed successfully
1 SELECT or FETCH statement returned no rows
2 DB2 command or SQL statement warning
4 DB2 command or SQL statement error
8 Command line processor system error

本文提供了一个使用DB2数据库进行连接和SQL查询的Shell脚本示例,演示了如何选择区域信息,并通过返回码判断操作是否成功。
3340

被折叠的 条评论
为什么被折叠?



