I've built a powershell script that allows me to run and email the result of sql queries that are being read from a sql file:
function global:readscript($path)
{
#using UTF7 encoding to allow select with accented/french/russian/chinese/... etc chars
$inenc = [System.Text.Encoding]::UTF7
$reader = new-object System.IO.StreamReader($path, $inenc)
$finalquery = ""
while ($line = $reader.ReadLine())
{
$finalquery += $line
}
$reader.close()
return $finalquery
}
function global:get-result($query)
{
$oracleconnection = new-object Oracle.ManagedDataAccess.Client.OracleConnection
$oracleconnection.connectionstring = $connectionstring
$oracleconnection.Open()
$oraclecommand = $oracleconnection.CreateCommand()
$oraclecommand.CommandText = $query
$reader = $oraclecommand.ExecuteReader()
#...etc
}
$scriptquery = readscript "d:\mysqlquery.sql"
get-result($scriptquery)
Everything is working fine so far, except this one sql script that contains the "+" sign for purpose of calculation.
Lets say file mysqlquery.sql contains a line such as:
(SELECT COUNT(a.ID)) + (SELECT COUNT(b.ID))
I can see in the console it's being translated to
(SELECT COUNT(a.ID)) (SELECT COUNT(b.ID))
and of course throws this annoying exception "missing right parenthesis"
How do I escape this plus sign when reading it from a txt file ?