Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

oracle - How to run a SQL Plus script in PowerShell

I am trying to log in to the the Oracle DB using PowerShell and run a script called "C:UsersAdministratorDesktoporacleOracleCleanTest.sql", When I execute the PS nothing happens.

Here is what I have.

$adminLogon = "sys as sysdba/manager@ORCL"
$logon = "sqlplussql/manager@ORCL"


$mydata = Invoke-SqlPlus -inputfile       "@C:UsersAdministratorDesktoporacleOracleCleanTest.sql" $logon

I've also tried this.

$database = "ORCL";
$user = "sys as sysdba";
$pw = "manager";

sqlplus.exe -d $database -U $user -P $pw -I "@C:UsersAdministratorDesktoporacleOracleCleanTest.sql"

I tried this.

& 'C:appAdministratorproduct11.2.0client_1BINsqlplus.exe' 'QE-JDBC-1/manager@ORCL sys as sysdba' '@C:UsersAdministratorDesktoporacleOracleCleanTest.sql'

I get the error, "& : The module 'sqlplus' could not be loaded. For more information, run 'Import-Module sqlplus'. At line:5 char:3 + & $mydata Invoke-SqlPlus -inputfile "@C:UsersAdministratorDesktoporacleOrac ... + ~~~~~~~ + CategoryInfo : ObjectNotFound: (sqlplussql/manager@ORCL:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CouldNotAutoLoadModule"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I use the call operator, &, as Keith Hill has suggested with the question, How to run an EXE file in PowerShell with parameters with spaces and quotes.

& 'pathsqlplus.exe' 'system/password@dbase as sysdba'

I placed the username, password in quotes due to the spaces.

To start a script, I add another parameter as follows:

 & 'pathsqlplus.exe' 'system/password@dbase as sysdba' '@my_script.sql'

If you are receiving the ORA-12154 error, and you know that other users have established connections (which implies that the database listener is running properly); I would then examine if SQL*Plus can find my tnsname file.

My first task would be to see if I can tnsping as follows in Windows cmd.exe:

tnsping orcl

It will confirm that a connection can (or can not be established).

If it cannot, I would check to see if the environment variable, ORACLE_HOME, is set. SQL*Plus uses this to find tnsname.ora file.

If it is not set, I would execute this statement in PowerShell (to establish this environment variable):

[Environment]::SetEnvironmentVariable("ORACLE_HOME", "C:appAdministratorproduct11.2.0client_1" , "User")

Next, I would retry to tnsping (identified above).

Once successful, I would re-try to execute the script running command above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...