用shell抽取db2的数据,并进行处理:
#SQL文定义
SQL="SELECT AAA, BBB, CCC FROM MYTBL1"
#执行SQL
SDATA=`db2 "$SQL"`
#返回值判断
if [ $? -ne 0 ]
then
#显示db2返回的错误信息
echo "$SDATA"
exit 1
fi
#对取得的数据进行处理。
echo "$SDATA" | sed -e '4,/^$/!d;/^$/d' |
while read AAA BBB CCC
do
echo "AAA IS $AAA, BBB IS $BBB, CCC IS $CCC"
done
#取得数据件数
echo "$SDATA" | sed -n -e '/^$/{1,3d;n;s/[^0-9]*\([0-9]*\)[^0-9]*/\1/;p;}' | read CNT
echo "The count of selected data is $CNT."
exit 0
◆更新db2的数据,并取得更新结果
SQL="UPDATE MYTBL1 SET AAA='2005',BBB='05',CCC='12'"
#执行SQL
SDATA=`db2 -a "$SQL"`
#取得SQLCODE
echo "$SDATA" | sed -n -e 's/^.*sqlcode: \([-,0-9][0-9]*\).*/\1/p' | read SQLCODE
echo "Sqlcode is $SQLCODE."
#取得SQLSTATE
echo "$SDATA" | sed -n -e 's/^.*sqlstate: \([-,0-9][0-9]*\).*/\1/p' | read SQLSTATE
echo "Sqlstate is $SQLSTATE."
#取得更新件数(即sqlerrd的第三个值)
echo "$SDATA" | sed -n -e '/sqlerrd/s/^.*(3) \([-,0-9][0-9]*\).*/\1/p' | read UPDCNT
echo "Updated data's count is $UPDCNT."
#取得sqlerrd的第五个值
echo "$SDATA" | sed -n -e '/sqlerrd/{n;s/^.*(5) \([-,0-9][0-9]*\).*/\1/;p;}' | read SQLERRD5
echo "Sqlerrd(5) is $SQLERRD5."