1

I have an Oracle 11 instance on Windows 2003 R2.

I run this to get the output as shown below:

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

1521/orcl

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

But, I want the output to just be 31, and not with the column header and the lines below it - something to the effect of SET HEADING OFF from within SQL*Plus.

I came across this question which suggested using the -e option for echo, but that either doesn't seem to work on Windows or I am missing something.

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.

This is the exact command mentioned in the post I referenced above:

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.

I do not see a SQL*Plus flag I can use (like -s I used above for silence) to turn heading off. Hence trying this method!

What can I do to make it work on Windows?

0

1 Answer 1

1

Bit messy, but you can do:

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

        53

The ^^^) part is to escape the parenthesis in the count(*), within the parentheses wrapping the two echo commands together - which provides a single input for SQL*Plus.

This has a blank line at the top; you might prefer to use set pagesize 0 instead of set heading off:

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

You can put multiple settings in one set command if you need to, as well.

Alternatively, just put all your commands in a script file, e.g. test.sql:

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

and then run that with:

sqlplus -s zabbix/pwd@localhost:1521/orcl @test
Sign up to request clarification or add additional context in comments.

5 Comments

The syntax with 'pages 0' works great. Thank you Alex!
Actually, the leading spaces in the output value doesn't work with zabbix for me.
I mean, instead of select count() from v$session; COUNT() -------------- 39 I need: select trim(count()) from v$session; COUNT() ------------- 39 When I use echo: (echo set pages 0 & echo select trim(LIMIT_VALUE) from v$resource_limit where RESOURCE_NAME='sessions'; & echo exit;) | "C:\Program Files\Zabbix\sqlplus.exe" -s zabbix/pwd@localhost:1521/orcl I get - 'from was unexpected at this time.' Any idea how to resolve this?
Did you escape the inner closing parenthesis again, with ^^^)
Just came here to say that it worked after escaping the ).. Thank you again Alex, really appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.