Shell执行DB2 SQL 返回值

http://migle.iteye.com/blog/1666580

Shell代码   收藏代码
  1. #!/usr/bin/env sh  
  2.   
  3. DBNODE='nodename'  
  4. DBUSER='username'  
  5. DBPASSWORD='password'  
  6.   
  7.   
  8. connDB2()  
  9. {  
  10.     if( db2 connect to $1  user $2 using $3 > /dev/null )  
  11.     then   
  12.         echo 'OK'  
  13.     else  
  14.         echo "failed connect to ${DBNODE}"  
  15.         exit -1  
  16.     fi  
  17. }  
  18.   
  19. releaseDB2(){  
  20.     db2 connect reset  > /dev/null  
  21. }  
  22.   
  23. run(){  
  24.     connDB2 ${DBNODE} ${DBUSER} ${DBPASSWORD}  
  25.     sql="  
  26.         select   
  27.             area_id ,areaname  
  28.         from   
  29.             pt.area  
  30.         "  
  31.       
  32.     db2 -x ${sql}| while read area_id areaname  
  33.     do  
  34.         echo "Result:${area_id}->${areaname}"  
  35.     done  
  36.     releaseDB2  
  37. }  
  38.   
  39. echo "execute sql ................."  
  40. run  

 如果有必要可以通过执行完SQL后$?的结果来判断是不是执行成功

 

 

Shell代码   收藏代码
  1. if [ "$?" -ne "0" ]  
  2.    then echo "execute error"  
  3.    exit -1  
  4. 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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值