我正在尝试编写一个脚本:
select count
是否存在我遇到的问题是在第一个查询运行后检查它是否存在,我不知道如何添加提示询问是否要插入数据库
到目前为止,这是我的代码:
echo "Please enter last name: 'input;"
read input
statement1="select count(*) as count from users
where fname = '"$input"'"
statement2="INSERT INTO users VALUES ('"$input"');
$ORACLE_HOME********************************
$statement1
/
quit;
Eossql
我无法弄清楚的部分是在执行语句1之后我希望系统询问是否应该执行语句2。有什么建议吗?
谢谢!
编辑: 这是我编辑的代码感谢CDahn的建议。这个解决方案有效,但我想知道是否有更简洁的方法来做这个而不是连接到sqlplus两次。
echo "Please enter last name: 'input;"
read input
Statement2="INSERT INTO users VALUES ('"$input"');"
output1=$($sqlplus -s User/Pass@connection <<EOF
set head off
select count (*) from users
where fname = '$input';
EXIT;
EOF
)
read -p "User $input appears $output1 times. Create user? [Y/n]" answer
if [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]
then
$sqlplus -s User/Pass@connection << Eossql
set autocommit on
set head off
$Statement2
quit;
Eossql
else
echo "Not creating user"
fi
答案 0 :(得分:2)
您需要添加对sqlplus
的调用以获取您已编写的查询的输出。请参阅https://www.brentozar.com/archive/2013/02/7-things-developers-should-know-about-sql-server/以获取相关帮助。
一旦你有输出,你可以使用条件,如拉曼Sailopal建议的那样。类似的东西:
output1="`sqlplus $statement1`"
read -p "User $input appears $output1 times. Create user? [Y/n]" answer
if [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]
then
sqlplus $statement2
else
echo "Not creating user."
fi