Mennan
Mennan

Reputation: 4497

How to execute sqlplus commands on cmd.exe?

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

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();

My sqlplus command text:

Set ORACLE_SID=prod
Set ORACLE_HOME=C:\oracle\product\10.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\10.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>Set ORACLE_HOME=C:\oracle\product\10.2.0\db_1

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

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

Upvotes: 1

Views: 61222

Answers (2)

CashWasabi
CashWasabi

Reputation: 119

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

Upvotes: 2

stefan.schwetschke
stefan.schwetschke

Reputation: 8932

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.

Upvotes: 1

Related Questions