ORACLE 将SQL的执行脚本返回值传给SHELL-Be the miracle!
遇到了一个这样的需求:用SHELL脚本去调用ORACLE中一个PACKAGE的FUNCTION,根据FUNCTION的返回值,SHELL需要判断接下来的操作。 其实调用一个过程或者函数还是比较简单的,但是捕捉各异的返回值进行判断就稍有不同了。后来我们的原型脚本:sqlplus -S "gsdb/gsdbpass" /dev/null variable dd number
whenever sqlerror exit sql.sqlcode
begin
:dd := gsdb.pkg_cpms.dbcheck_1('123','67');
raise_application_error(-20224-:dd,'some errors!');
end;
/
exit;
!
export test=$?
echo $test
注意:脚本中"$?"可能会因为不同的UNIX或LINUX版本不同而稍有差别。或是$status或是$dd
注意关键的地方:
To return a status code of over 0 (which usually indicates no error) up to 255, you can use the "raise_application_error" function, passing -20224-status to it, along with the "whenever sqlerror" directive, to return the code back to the UNIX shell. 好的,我们可以轻松的将SQL脚本执行的返回值让SHELL得到了,但是不要忘记范围:0--255,虽然我们不会用那么多返回值。不难看出ORACLE还有多少这样的小秘密呢 -:)
页:
[1]