SQLPLUS - 来自命令行的多个 sql 语句

SQLPLUS - Multiple sql statements from command line

我在 Windows 2003 R2 上有一个 Oracle 11 实例。

我运行这样得到如下图的输出:

C:\>echo select count(*) from v$session; | sqlplus -s zabbix/pwd@localhost:

1521/orcl

  COUNT(*)
  ----------
       31

但是,我希望输出只是 31,而不是列 header 和它下面的行 - 在 SQL* 中达到 SET HEADING OFF 的效果加上.

我发现 this question 建议对 echo 使用 -e 选项,但这似乎对 Windows 不起作用,或者我遗漏了一些东西.

C:\>echo -e "set heading off; \n select count(*) from v$session;" | sqlplus -s zabbix/pwd@localhost:1521/orcl

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

这是我在上面引用的 post 中提到的确切命令:

C:\>echo -e "select 1 from dual; \n select 2 from dual;" | sqlplus -s zabbix/pwd@localhost:1521/orcl

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

我没有看到我可以使用的 SQL*Plus 标志(例如 -s 我在上面用于静音)来关闭航向。因此尝试这个方法!

我该怎么做才能让它在 Windows 上运行?

有点乱,但你可以这样做:

C:>(echo set heading off & echo select count(*^^^) from v$session; & echo exit;) | sqlplus -s zabbix/pwd@localhost:1521/orcl

        53

count(*) 中的 ^^^) 部分 is to escape the parenthesis,在括号内将两个 echo 命令包装在一起 - 它为 SQL*Plus 提供了一个输入。

上面有一个空行;你可能更喜欢使用 set pagesize 0 而不是 set heading off:

C:>(echo set pages 0 & echo select count(*^^^) from v$session; & echo exit;) | sqlplus -s zabbix/pwd@localhost:1521/orcl
        53

如果需要,您也可以将多个设置放在一个设置命令中。

或者,只需将所有命令放在一个脚本文件中,例如test.sql:

set pages 0
select count(*) from v$session;
exit

然后 运行 与:

sqlplus -s zabbix/pwd@localhost:1521/orcl @test