65

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

1

5 Answers 5

60

Just be aware that on Unix/Linux your username/password can be seen by anyone that can run "ps -ef" command if you place it directly on the command line . Could be a big security issue (or turn into a big security issue).

I usually recommend creating a file or using here document so you can protect the username/password from being viewed with "ps -ef" command in Unix/Linux. If the username/password is contained in a script file or sql file you can protect using appropriate user/group read permissions. Then you can keep the user/pass inside the file like this in a shell script:

sqlplus -s /nolog <<EOF
connect user/pass
select blah;
quit
EOF
Sign up to request clarification or add additional context in comments.

5 Comments

not always true, depending on Oracle version and OS, but probably a good standard to follow
What wonders me is why commandline leaking is not simply tackeled in the sql client program itself, as that would be quite trivial to do: in the main() function, copy the commandline args to a new variable, and overwrite the old args/argv[], and no commandline is visible (or - just for a fraction of a millisecond perhaps) - why doesn't oracle implement that solution?
@RobHeusdens because it's not Oracle's problem? Why should Oracle assume that users shouldn't see what they expect to see?
@DavidMann is this supposed to be this? sqlplus -s /nolog <<EOF connect username/password@database_name select 1 from dual; quit EOF Not sure where to put the database name.
57

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.

7 Comments

+1, but be aware that if you're doing anything moderately complicated in SQL*Plus you'll usually be making a few SET directives and this will get messy if you're looking to have a one-line command.
+1 Finally, the answer! Adding the semi-colon to the end of the query! You actually can run sqlplus, log in, and then run the commands - you don't have to pipe in the credentials. My problem was the commands weren't running - the line numbers just kept incrementing! Now I know how to get them to run -- just add a semi-colon! Thanks!
+1 Nice! I used this for a desc table_name; command and was getting unknown command errors, but removed the quotes and it worked. echo desc table_name; | sqlplus uid/pwd@host
The quotes produce an Oracle "SP2-0734: unknown command beginning ""select 1 ..." error for me on Windows. Removing them works for me.
works for docker as well: docker exec -it CONTAINER_ID bash -c "echo 'select table_name from user_tables;' | sqlplus username/password@//localhost:1521/ORCLCDB";. Lesson is dont forget ; in statement to execute
|
34

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

3 Comments

and how can we use statement stored in a variable in first version with <<<? I got the error.
@rofrol what version are you using? Not able to do the multiline one in oracle.
@noobprogrammer1987 I answered it in 2013 year. I haven't use Oracle for a while :)
9

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


$

Comments

4

For windows version you can try below. remove double quotes (")

echo select 1 from dual; | sqlplus -s username/password@service

Comments

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.