【问题标题】:how to write sql query in if condition of ksh shell script如何在ksh shell脚本的if条件下编写sql查询
【发布时间】:2018-03-17 22:30:46
【问题描述】:

我对 shell 脚本非常陌生,我想在 IF '条件'中编写 SQL 查询,而不是在 if '块'中。谁能帮我解决这个问题?

我的代码:-

if [sqlplus -s /@user_pass_dbname << EOF select param_value from my_table where my_id=1 EXIT; /EOF == '7878'];then
echo "works!!"
else
echo "doesn't work"

输出:- 第 3 行的语法错误:`

【问题讨论】:

    标签: sql oracle shell unix ksh


    【解决方案1】:

    改为在变量中从 sqlplus 中获取值:

    result=`sqlplus -s /@user_pass_dbname <<EOF
    set head off feedback off
    select param_value from param_table where param_id=1;
    exit
    EOF`
    
    if [ $result = 965 ]; then
     echo "works"
    else
     echo "doesn't work"
    fi
    

    【讨论】:

    • 它不正确,所以我附和了 $result。它说 PARAM_VALUE ----------------------------------------------- --------------------------------- 965 所以,它正在打印表格本身,而不仅仅是值。跨度>
    • 你是否添加了头部并关闭了反馈?
    【解决方案2】:

    我现在没有环境可以测试,但我认为你需要加上引号(')。

        'sqlplus -s /@user_pass_dbname << EOF select param_value from my_table 
        where my_id=1 EXIT; /EOF'
    

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 2016-10-25
      • 2017-10-08
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2013-11-21
      相关资源
      最近更新 更多