How can I issue a single command from the command line through sql plus?

OracleCommand LineSqlplus

Oracle Problem Overview


Using SQL Plus, you can run a script with the "@" operator from the command line, as in:

c:\>sqlplus username/password@databasename @"c:\my_script.sql"

But is it possible to just run a single command with a similar syntax, without a whole separate script file? As in:

c:\>sqlplus username/password@databasename @execute some_procedure

I am interested in this because I want to write a batch file that simply executes a command, without generating a bunch of two-line ".sql" files.

Oracle Solutions


Solution 1 - Oracle

I'm able to run an SQL query by piping it to SQL*Plus:

@echo select count(*) from table; | sqlplus username/password@database

Give

@echo execute some_procedure | sqlplus username/password@databasename

a try.

Solution 2 - Oracle

Have you tried something like this?

sqlplus username/password@database < "EXECUTE some_proc /"

Seems like in UNIX you can do:

sqlplus username/password@database <<EOF
EXECUTE some_proc;
EXIT;
EOF

But I'm not sure what the windows equivalent of that would be.

Solution 3 - Oracle

For UNIX (AIX):

export ORACLE_HOME=/oracleClient/app/oracle/product/version
export DBUSER=fooUser
export DBPASSWD=fooPW
export DBNAME=fooSchema 

echo "select * from someTable;" | $ORACLE_HOME/bin/sqlplus $DBUSER/$DBPASSWD@$DBNAME

Solution 4 - Oracle

sqlplus user/password@sid < sqlfile.sql

This will also work from the DOS command line. In this case the file sqlfile.sql contains the SQL you wish to execute.

Solution 5 - Oracle

@find /v "@" < %0 | sqlplus -s scott/tiger@orcl & goto :eof

select sysdate from dual;

Solution 6 - Oracle

This is how I solved the problem:

<target name="executeSQLScript">
	<exec executable="sqlplus" failonerror="true" errorproperty="exit.status">
		<arg value="${dbUser}/${dbPass}@<DBHOST>:<DBPORT>/<SID>"/>
		<arg value="@${basedir}/db/scripttoexecute.sql"/>
	</exec>
</target>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosephStyonsView Question on Stackoverflow
Solution 1 - OraclePatrick CuffView Answer on Stackoverflow
Solution 2 - OracleEric PetroeljeView Answer on Stackoverflow
Solution 3 - OraclejavaPlease42View Answer on Stackoverflow
Solution 4 - Oracletale852150View Answer on Stackoverflow
Solution 5 - OraclexxoidView Answer on Stackoverflow
Solution 6 - OraclejkobView Answer on Stackoverflow