How to run a SQL Plus script in PowerShell

后端 未结 5 1910
鱼传尺愫
鱼传尺愫 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:03

    In your Windows PowerShell command prompt the code does not require variable setting or anything fancy. Just do this:

    sqlplus ElBankoUser\SupaSecretyPass "@C:\Users\ElBankoUser\Documents\MaFancySckrp.sql"

    0 讨论(0)
  • 2020-12-15 00:06

    You can use .NET Oracle library DLL, just make sure you have the required DLL file under the lib folder

    Add-Type -Path "lib\Oracle.ManagedDataAccess.dll"
    
    $query = "select  1 as Col1, 2 as Col2, 3 as Col3 from dual
              union
              select  4 as Col1, 5 as Col2, 6 as Col3 from dual
              union
              select  7 as Col1, 8 as Col2, 9 as Col3 from dual"
    
    $cn   = New-Object Oracle.ManagedDataAccess.Client.OracleConnection -ArgumentList "TNS-ConnectionString-Here"
    $cmd  = New-Object Oracle.ManagedDataAccess.Client.OracleCommand    -ArgumentList $query
    
    $cmd.Connection = $cn
    
    try {
        $cn.Open()
    
        $reader = $cmd.ExecuteReader()
    
        while ($reader.Read()) {
            $col1 = $reader["Col1"]
            $col2 = $reader["Col2"]
            $col3 = $reader["Col3"]
    
            Write-Host $col1, $col2, $col3
        }
    
    } catch {
        Write-Error $_.Exception.Message
    } finally {
        $cmd.Dispose()
        $cn.Dispose()
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 00:13

    I use this:

    $cmd = "cmd.exe"
    $args = ("/c sqlplus {0}/{1}@{2}:{3}/{4} @{5} {6}" -f $userName, $password, $tnsAlias, $port, $dbInstance, $sqlScript, $outputFileName)
    &$cmd $args 
    
    0 讨论(0)
  • 2020-12-15 00:22

    Why not use this?

    sqlplus -s $adminLogin "@C:\Users\Administrator\Desktop\oracle\OracleCleanTest.sql"

    -s just suppresses the sqlplus banner.

    0 讨论(0)
提交回复
热议问题