暂无图片
在shell中操作sqlplus,如何实现每查询一条就将查询的字段与结果返回给shell变量?
我来答
分享
清晨到日暮
2021-10-09
在shell中操作sqlplus,如何实现每查询一条就将查询的字段与结果返回给shell变量?

在shell中操作sqlplus,如何实现每查询一条就将查询的字段与结果返回给shell变量?

我来答
添加附件
收藏
分享
问题补充
1条回答
默认
最新
Murkey

注意sqlplus段使用老板键`了, 赋变量的等号两侧不能有空格.

$ vi test2.sh

#!/bin/bash
VALUE=`sqlplus -S /nolog <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
select count(*) from tab;
exit
EOF`
if [ "$VALUE" -gt 0 ]; then
        echo "The number of rows is $VALUE."
        exit 0
else
        echo "There is no row in the table."
fi

$ chmod +x test2.sh
$ ./test2.sh

三、把sqlplus执行结果传递给shell方法二

注意sqlplus段使用 col .. new_value .. 定义了变量并带参数exit, 然后自动赋给了shell的$?

$ vi test3.sh

#!/bin/bash
sqlplus -S /nolog > result.log <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
col coun new_value v_coun
select count(*) coun from tab;
exit v_coun
EOF
VALUE="$?"
echo "The number of rows is $VALUE."

$ chmod +x test3.sh
$ ./test3.sh

四、把shell程序参数传递给sqlplus

$1表示第一个参数, sqlplus里可以直接使用, 赋变量的等号两侧不能有空格不能有空格.

$ vi test4.sh

#!/bin/bash
NAME="$1"
sqlplus -S u_test/iamwangnc <<EOF
select * from tab where tname = upper('$NAME');
exit
EOF

$ chmod +x test4.sh
$ ./test4.sh ttt

暂无图片 评论
暂无图片 有用 0
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
暂无图片墨值悬赏