oracle 如何通过 sql plus 从命令行发出单个命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/638705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:45:50  来源:igfitidea点击:

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

oraclecommand-linesqlplus

提问by JosephStyons

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

使用 SQL Plus,您可以从命令行运行带有“@”运算符的脚本,如下所示:

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

But is it possible to just run a single commandwith 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.

我对此很感兴趣,因为我想编写一个批处理文件,它只执行一个命令,而不生成一堆两行“.sql”文件。

回答by Patrick Cuff

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

我可以通过将 SQL 查询通过管道传输到 SQL*Plus 来运行它:

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

Give

@echo execute some_procedure | sqlplus username/password@databasename

a try.

一试。

回答by Eric Petroelje

Have you tried something like this?

你有没有尝试过这样的事情?

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

Seems like in UNIX you can do:

似乎在 UNIX 中你可以这样做:

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

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

但我不确定 Windows 的等价物是什么。

回答by javaPlease42

For UNIX (AIX):

对于 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

回答by tale852150

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.

这也适用于 DOS 命令行。在这种情况下,文件 sqlfile.sql 包含您希望执行的 SQL。

回答by xxoid

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

select sysdate from dual;

回答by jkob

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>