oracle 如何在 cmd.exe 上执行 sqlplus 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18852272/
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 05:17:29  来源:igfitidea点击:

How to execute sqlplus commands on cmd.exe?

oraclecommand-lineprocesscommand-line-argumentssqlplus

提问by Mennan

My sqlplus command doesnt finish , it wait on password line,it dont give me error.

我的 sqlplus 命令没有完成,它在密码线上等待,它没有给我错误。

This code doesnt problem when i open new cmd window and paste this code ! everything is ok, but i cant run on c# using process();

当我打开新的 cmd 窗口并粘贴此代码时,此代码没有问题!一切正常,但我无法使用 process() 在 c# 上运行;

My sqlplus command text:

我的 sqlplus 命令文本:

Set ORACLE_SID=prod
Set ORACLE_HOME=C:\oracle\product.2.0\db_1
sqlplus -s 
sys as sysdba
password
shutdown immediate
startup mount
recover standby database until cancel;
cancel
alter database open read only;
exit;
exit;

I try this :

我试试这个:

C:\cmd.cmd
*********************************************
Set ORACLE_SID=prod
Set ORACLE_HOME=C:\oracle\product.2.0\db_1
sqlplus -s 
sys as sysdba
manager
select * from dual;
exit;
*********************************************

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\cmd.cmd";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;

process = Process.Start(startInfo);

//output
string BatProcessResult = process.StandardOutput.ReadToEnd();

Output:

输出:

D:\AppPath\bin\Debug>Set ORACLE_SID=prod

D:\AppPath\bin\Debug>设置 ORACLE_SID=prod

D:\AppPath\bin\Debug>Set ORACLE_HOME=C:\oracle\product\10.2.0\db_1

D:\AppPath\bin\Debug>设置 ORACLE_HOME=C:\oracle\product\10.2.0\db_1

D:\AppPath\bin\Debug>sqlplus -s

D:\AppPath\bin\Debug>sqlplus -s

D:\AppPath\bin\Debug>sys as sysdba;

D:\AppPath\bin\Debug>sys as sysdba;

采纳答案by stefan.schwetschke

Try

尝试

sqlplus -L username/pw@db @ fileWithCommands.sql

or

或者

set ORACLE_SID=...
sqlplus -L / as sysdba

This will not wait for a username and password but log you in with the given credentials. If they are wrong, sqlplus will exit immediately with an error. If the credentials are correct, sqlplus will run all the commands in the given file, one after the other.

这不会等待用户名和密码,而是使用给定的凭据登录。如果它们是错误的,sqlplus 将立即退出并显示错误。如果凭据正确,sqlplus 将一个接一个地运行给定文件中的所有命令。

回答by CashWasabi

You forgot to append the user role to your command. Try to use this one

您忘记将用户角色附加到您的命令中。尝试使用这个

sqlplus <username>/<password>@<host>:<port>/<sid> <db_role> @<script>

for example:

例如:

sqlplus sys/oracle@localhost:1524/ORCL AS SYSDBA @myscript.sql