0

请参考我的示例,在 oracle DB 中,这些在 SP 下面

CREATE OR REPLACE PROCEDURE SP_TEST_PUTLINE AS 
BEGIN
DBMS_OUTPUT.ENABLE;
  dbms_output.put_line('Hello world!');
END SP_TEST_PUTLINE;

我使用unix shell调用SPdbms_output.put_line('Hello world!')从SP获取,我该怎么办。我使用 sqlplus 命令登录数据库,似乎无法得到我想要的结果。

   output=$(IFS='';echo connect ${DBUSER}/${DBPASS}@${ORACLE_SID} execute SP_TEST_PUTLINE|sqlplus -s /nolog )

谁能帮我?提前致谢...

------------------------------------分割线20180705 ----------- --------------------------

感谢 Alex 和 Kaushik Nayak 的帮助,非常有帮助。

以下是 Kaushik Nayak 的一些发现,请参考。

嗨 Kaushik,它可以工作,但起初,它失败了,出现以下错误

unknown command beginning "-e connect..." - rest of line ignored. SP2-0734: 

所以我将 echo -e 选项更改为 echo,然后它就可以工作了。所以这里有问题1)为什么我使用没有-e选项的回声会得到低于结果,是IFS引起的吗?

echo "abc\n def \nghi" 
abc
 def 
ghi

2)根据Alex的评论,这两个选项需要写成两行吗?但你没有在他们之间使用 \n

 set serveroutput on
set feedback off

当我在它们之间添加 \n 时,如下所示。它遇到了如下错误,但是'hellow world!' 有输出,

SP2-0734: unknown command beginning "feedback o..." - rest of line ignored. Hello world! PL/SQL procedure successfully completed. 

这里有个问题,为什么你没有在 set serveroutput on 和 set feedback off 之间使用 \n,这个 DB 命令(set feedback off)运行成功了吗?

期待你的回复。提前致谢!

4

2 回答 2

2

如果您使用单行回显传递给 sqlplus,则应使用-e选项添加换行符echo

output=$(IFS='';echo -e "connect  ${DBUSER}/${DBPASS}@${ORACLE_SID}\nset serveroutput on feedback off\n execute SP_TEST_PUTLINE" |sqlplus -s /nolog )

您还必须指定set serveroutput onfeedback off仅查看输出。

因此,更好的选择是使用 Alex 指出的此处文档。

于 2018-07-04T10:31:27.967 回答
2

您正在使用的命令甚至无法连接,因为您需要在connect和之间换行execute

但你也需要set serveroutput on,而且你可能也想这样做set feedback off(也许还有其他选择。

我会使用heredoc来使其更易于阅读和维护:

output=$(
sqlplus -s /nolog <<!EOF
connect ${DBUSER}/${DBPASS}@${ORACLE_SID}
set serveroutput on
set feedback off
execute SP_TEST_PUTLINE
!EOF
)

# then do whatever you want with the output
echo ${output}

您可能还想做一些错误检查...

于 2018-07-04T10:28:27.733 回答