Is it possible to do something like this?
$ sqlplus -s user/pass "select 1 from dual"
or
$ echo "select 1 from dual" | sqlplus -s user/pass
I know I can put select 1 from dual
in a file and do this:
$ sqlplus -s user/pass @myFile.sql
but I'm wondering if it's actually necessary to create a file just to satisfy sqlplus
My version
$ sqlplus -s username/password@host:port/service <<< "select 1 from dual;"
1
----------
1
EDIT:
For multiline you can use this
$ echo -e "select 1 from dual; \n select 2 from dual;" | sqlplus -s username/password@host:port/service
1
----------
1
2
----------
2
I'm able to execute your exact query by just making sure there is a semicolon at the end of my select statement. (Output is actual, connection params removed.)
echo "select 1 from dual;" | sqlplus -s username/password@host:1521/service
Output:
1
----------
1
Note that is should matter but this is running on Mac OS X Snow Leopard and Oracle 11g.
I assume this is *nix?
Use "here document":
sqlplus -s user/pass <<+EOF
select 1 from dual;
+EOF
EDIT: I should have tried your second example. It works, too (even in Windows, sans ticks):
$ echo 'select 1 from dual;'|sqlplus -s user/pw
1
----------
1
$
Source: Stackoverflow.com