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

145,152

Solution 1

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

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

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

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.

Share:
145,152
JosephStyons
Author by

JosephStyons

I started out as a professional developer using Delphi and Oracle in a Win32 client-server environment for a manufacturing company. I worked for five years in consulting, implementing solutions for dozens of clients and using many disparate technologies. Since then, I've worked for and with the non-profit industry, building applications that help them move their missions forward. My bread-and-butter is VB.NET and C# against a SQL Server back-end using a SOA architecture. But I can and will use whatever tool gets the job done, and I've had fun doing so with Angular, jQuery, ASP.NET, PHP, and even my own homemade frameworks to deliver solutions against that platform.

Updated on July 27, 2022

Comments

  • JosephStyons
    JosephStyons almost 2 years

    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.

  • JosephStyons
    JosephStyons about 15 years
    The < symbol is a pipe in Windows too. Nice idea, but it does not work.
  • spencer7593
    spencer7593 almost 15 years
    that command line will be visible to other os users with (e.g.) ps -ef, if you have to include username and password, better to execute "sqlplus /nolog <<EOF" and have the first line be "connect username/password". Not the ideal solution, but it's better in that at least it doesn't expose the clear text username and password in output from ps -ef
  • ivanatpr
    ivanatpr over 11 years
    What does the @ symbol before echo do in this case?
  • Patrick Cuff
    Patrick Cuff over 11 years
    @IvanG; For Windows batch files, the @ symbol before a command surpress the command from being echoed to the console. It's useful in situations where you don't want to turn echo off, but don't want to see the command echoed on the screen. In this case it's needed so that what gets piped to the sqlplus command is the SQL we want to run, not the command text.
  • Ravimallya
    Ravimallya almost 9 years
    Describe your answer more specifically where to set this and how it will fix the error etc.
  • maxshuty
    maxshuty over 8 years
    Please add more details do your answer.
  • durette
    durette almost 5 years
    This solution in Apache Ant still needs a SQL script scripttoexecute.sql stored separately from the calling script.
  • msm1089
    msm1089 about 4 years
    OP already said he knows he can do this. He is asking for a way to run a single command without needing a separate SQL script.