Config SQL*Plus to return nothing but data

人盡茶涼 提交于 2019-11-30 22:49:46

There are a few different approaches in this askTom thread on returning values from SQL*Plus to a shell script.

One common approach is to select a constant token in addition to the value that you want to return (in Tom's example, that is the string "KEEP") and then use sed (or your favorite command-line parser) to extract the data you're actually interested in

#!/bin/ksh

x=`sqlplus / <<endl | grep KEEP | sed 's/KEEP//;s/[   ]//g'
select 'KEEP' , max(sal) from emp;
exit
endl`

echo the answer is $x

Other approaches, such as approaches that allow you to read multiple lines of output are also discussed in that thread.

If you don't want the header to be printed, you should be specifying

set head off

in your SQL*Plus script-- I'm not sure why you're explicitly setting the header on in the script if you don't want the header... Do you want to keep some part of the header?

Based on Justin's answer (which is great), when you only need to select one number (or token), I consider this a little shorter, yet more readable version:

x=`sqlplus -S / <<< "select 'KEEP' , max(sal) from emp;" | awk '/KEEP/{print $2}'`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!