How to run a SQL Plus script in PowerShell

后端 未结 5 1912
鱼传尺愫
鱼传尺愫 2020-12-14 23:36

I am trying to log in to the the Oracle DB using PowerShell and run a script called \"C:\\Users\\Administrator\\Desktop\\oracle\\OracleCleanTest.sql\", When I execute the PS

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 00:07

    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.

    & 'path\sqlplus.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:

     & 'path\sqlplus.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:\app\Administrator\product\11.2.0\client_1" , "User")
    

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

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

提交回复
热议问题