在不使用提示或脚本的情况下回显 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`

当前输出:

$ 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

尝试 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

当 SQL*Plus 从您的 TTY 读取命令时,输入的回显实际上是由您的 TTY 处理的,而不是 SQL*Plus。如果 SQL*Plus 处理回显,任何时候你手动输入一个命令,你都会看到该命令两次(一次是你输入的,一次是回显的)。

此外,TERMOUT 选项仅适用于 运行 脚本文件,而不是从 STDIN 读取。

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

sqlplus scott/tiger @/dev/stdin <<EOF
SET TERMOUT ON ECHO ON
SELECT SYSDATE FROM dual;
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