繁体   English   中英

在不使用提示或脚本的情况下回显 sqlplus 命令

[英]Echo sqlplus command without using prompts or scripts

我有一个我想作为单个文件维护的脚本,但是我希望在不使用PROMPT <sql>@script.sql输入到 sqlplus 中的命令,这可以完成吗?

当前脚本:

$ cat test.sh
#!/bin/bash
LOG=/home/oracle/output.log
sqlplus hr/hr <<EOF > $LOG
set echo on
select 1 from dual;
QUIT
EOF`

当前output:

$ cat output.log
SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 1 15:01:12 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options

SQL> SQL>
         1
----------
         1

我想要的是:

$ cat output.log

SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 1 15:02:02 2016

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options

SQL> SQL> SQL> select 1 from dual;

         1
----------
         1

当SQL * Plus从TTY中读取命令时,输入的回声实际上是由TTY处理的,而不是由SQL * Plus处理的。 如果SQL * Plus处理了回显,则每次您手动键入命令时,都会看到该命令两次(键入一次,回显一次)。

此外, TERMOUT选项仅在运行脚本文件时适用,而不能从STDIN中读取。

简单的解决方法是告诉SQL * Plus /dev/stdin是一个脚本:

sqlplus scott/tiger @/dev/stdin <<EOF
SET TERMOUT ON ECHO ON
SELECT SYSDATE FROM dual;
EOF

尝试spool而不是重定向您的STDOUT:

#!/bin/bash
LOG=/home/oracle/output.log
sqlplus hr/hr <<EOF
set echo on term on 
spool $LOG
select 1 from dual;
QUIT
EOF

扩展精彩的@Mr.llama 答案。 他的构造在 Solaris 上运行完美。

但是在 Linux(Oracle Linux 服务器版本 7.9)上,这会导致每个命令在 sqlplus 中运行两次,即使它只回显命令一次。 见下文:

$ sqlplus -l / as sysdba @/dev/stdin <<-EOF
>     set echo on termout on
>     select systimestamp from dual;
> EOF


SQL*Plus: Release 19.0.0.0.0 - Production on Wed Feb 23 14:33:03 2022
Version 19.11.0.0.0

Copyright (c) 1982, 2020, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0

SQL>     select systimestamp from dual;

SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.33.03.975542 PM -08:00

SQL> SQL>
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.33.03.976106 PM -08:00

SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0

请注意有两条 systimestamp 行,它们具有不同的时间戳,这意味着 select 被执行了两次。

当同一块在 Solaris 上运行时,不会发生这种双重执行。

Linux 解决方法是将构造修改为:

$ cat /dev/stdin <<-EOF | sqlplus -l / as sysdba @/dev/stdin
>     set echo on termout on
>     select systimestamp from dual;
> EOF

SQL*Plus: Release 19.0.0.0.0 - Production on Wed Feb 23 14:38:08 2022
Version 19.11.0.0.0

Copyright (c) 1982, 2020, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0

SQL>     select systimestamp from dual;

SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.38.08.931790 PM -08:00

SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM