====== Mit der PowerShell Oracle SQL*Plus aufrufen und Daten aus der Oracle Datenbank auslesen ====== **03.2014 Update 12.2017** In Unix kann über Backtick und Pipe nach SQL*Plus ein Kommando in SQL*Plus abgesetzt werden und das Ergebnis in einem Skript verarbeitet werden. \\ \\ Beispiel für Linux Bash Skript: #check Version of Database # Workaround for the $ in DB View Names VERVIEW=\$version #Call SQL*Plus ISENTERISE=`echo "set pagesize 0 set feedback off select count(*) from v_${VERVIEW} where banner like '%Enterprise%'; quit"|${ORACLE_HOME}/bin/sqlplus -s / as sysdba` echo "check DB Version - Get 1 for EE and 0 for SE - Result is ${ISENTERISE}" # oder alternativ wenn keine Rückgabe notwendig ist ${ORACLE_HOME}/bin/sqlplus / as sysdba << EOScipt CREATE pfile='${BACKUP_DEST}/${ORACLE_DBNAME}/init_${ORACLE_DBNAME}_${DAY_OF_WEEK}.ora' FROM spfile; exit; EOScipt \\ Ähnlich kann dies auch in der PowerShell erfolgen: \\ $isenterprise=@' set pagesize 0 set feedback off select count(*) from v_$version where banner like '%Enterprise%'; quit '@| & "$env:ORACLE_HOME/bin/sqlplus" -s / as sysdba $isenterprise=$isenterprise.Trim() write-host "Info -- check DB Version - Get 1 for EE and 0 for SE - Result is ::" $isenterprise Der Trick liegt in den **@' '@** String Konstrukt und dem **&** zum Aufruf von SQL*Plus mit Pfad über die Umgebungsvariable ORACLE_HOME. Die Ausgabe von SQL*Plus steht dann in der Variablen isenterprise, Leerzeichen mit der Methode trim entfernen. !Achtung: Sollen Variablen innerhalb der **@' '@** String Konstrukt ausgewertet werden muss mit **@““@** für die String Ersetzung gearbeitet werden. ==== SQL*Plus mehrfach für eine Umgebung über eine Schleife aufrufen ==== Gelegentlich soll über eine ganze Umgebung von Datenbanken das gleiche Skript aufgerufen werden. Da in dieser Umgebung das Passwort ein Dollar Zeichen enthält muss dieser Dollar extra "geschützt" werden. Dazu wird die Variable mit dem einfachen hoch Komma definiert, dann wird das $ nicht im String ausgewertet. Beispiel: $DBS = @("GPIDB1","GPIDB2","GPIDB3","GPIDB4","GPIDB5"); # # use ' to escape the Dollar sign! # $PWD='PASSWORD_MIT_$_SIGN' $COMMAND="database.sql" foreach ($element in $DBS) { $ARGUMENTS="system/$PWD@$element @$COMMAND" echo "-- Info -- Start sqlplus with this $ARGUMENTS" ; start-Process -FilePath sqlplus -ArgumentList "$ARGUMENTS" ; } ===== Daten als CSV Datei schreiben und wieder einlesen ==== ===Auslesen=== Variante 12c R2 SQL*Plus mit der „set markup csv“ Formatierung: #============================================================================== # Author: Gunther Pippèrr ( http://www.pipperr.de ) # Desc: Library for the Oracle Data import / Export scripts # Date: 01.Dezember 2017 # Site: http://orapowershell.codeplex.com #============================================================================== <# .NOTES Created: 11.2017 : .SYNOPSIS read data to the oracle database with sqlplus .DESCRIPTION read data to the oracle database with sqlplus .COMPONENT Oracle PS Helper Scripts #> #============================================================================== # Enviroment #============================================================================== Set-Variable CONFIG_VERSION "0.2" -option constant # Rember the script path to find the csv $Invocation = (Get-Variable MyInvocation -Scope 0).Value $scriptpath=Split-Path $Invocation.MyCommand.Path $starttime=get-date write-host "Info -- start the Script in the path $scriptpath at $starttime" -ForegroundColor "green" cd $scriptpath # Set working directory if executed as job # Set enviroment $oracle_home="C:\oracle\instantclient_12_2" $sql_connect_string="gpi/gpi@gpidb" $data_csv="$scriptpath\userPWDImport.csv" #set Oracle HOME variable try { set-item -path env:ORACLE_HOME -value $oracle_home } catch { new-item -path env: -name ORACLE_HOME -value $oracle_home } #read input $input_csv = Get-Content "$data_csv" # loop over each line foreach( $line in $input_csv ) { #debug write-host $line $line="'$line'" write-host $line # Write the data with sqlplus 12c R2! (set markup csv on quote off!) # for all other use the older format settings! # must be on start of line!! $writeData=@" set pagesize 0 set feedback off set markup csv on quote off set heading off select $line as val1, sysdate as val2,sysdate+1 as val3 from dual; quit "@| & "$env:ORACLE_HOME\sqlplus" -s "$sql_connect_string" $writeData=$writeData.trim() #DEBUG write-host $writeData } #write to file $readData | Out-File -FilePath "$data_csv" -append #============================= End of File ==================================== Das Password sollte natürlich hier nicht in Klarschrift in einem produktiven Skript stehen! ===Einlesen der CSV Daten=== SQL Beispiel mit einem Update #============================================================================== # Author: Gunther Pippèrr ( http://www.pipperr.de ) # Desc: Library for the Oracle Data import / Export scripts # Date: 01.Dezember 2017 # Site: http://orapowershell.codeplex.com #============================================================================== <# .NOTES Created: 11.2017 : .SYNOPSIS write data to the oracle database with sqlplus .DESCRIPTION write data to the oracle database with sqlplus .COMPONENT Oracle PS Helper Scripts #> #============================================================================== # Environment #============================================================================== Set-Variable CONFIG_VERSION "0.2" -option constant # Rember the script path to find the csv $Invocation = (Get-Variable MyInvocation -Scope 0).Value $scriptpath=Split-Path $Invocation.MyCommand.Path $starttime=get-date write-host "Info -- start the Script in the path $scriptpath at $starttime" -ForegroundColor "green" cd $scriptpath # Set working directory if executed as job # My local Settings $oracle_home="C:\oracle\products\12.2.0.1\client_64" $sql_connect_string="gpi/gpi@gpidb" $data_csv="$scriptpath\userDataImport.csv" # set the Oracle HOME variable try { set-item -path env:ORACLE_HOME -value $oracle_home } catch { new-item -path env: -name ORACLE_HOME -value $oracle_home } #============================================================================== #read the data input $input_csv = Get-Content "$data_csv" # loop over each line of the data input foreach( $line in $input_csv ) { #debug #write-host $line #check for empty lines and skip this lines for ($line.length() -le 5) { # exit from this iteration round continue } # split the csv # $val1,$val2,$val3 = $line.split('|') #debug write-host $val1 write-host $val2 write-host $val3 $command="update dataTab set val1='$val1' , val2='$val2' where val3='$val3'" # Write the data with sqlplus # must be on start of line!! $writeData=@" set pagesize 0 set feedback off $command commit; quit "@| & "$env:ORACLE_HOME\sqlplus" -s "$sql_connect_string" # trim the response try { $writeData=$writeData.trim() } catch { write-host "Object is empty - check your code" } #DEBUG write-host $writeData } $stoptime=get-date write-host "Info -- finsh the Script at $stoptime" -ForegroundColor "green" #============================= End of File ==================================== Das Password sollte natürlich hier nicht in Klarschrift in einem produktiven Skript stehen!