I have a script that executes SQL files and prints the result in the console (for now). The problem is that I need to distinguish between NULL values and empty rows that are returned in the result table.
This is what the query returns in Studio Management:

You can see that it contains strings, empty strings and NULL values.
This is what the query returns in PowerShell:

There is no difference between NULL and empty.
In addition, the last few columns are cut out and only the first 10 are printed.
How can i fix this?
Here is my code:
$ConnectionString = "Data Source=...;Initial Catalog=...;User Id=..;Password=.."
$FolderToSQLFiles = "C:\SQLFilesTestFolder";
function GetSQLresults {
Param(
[Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, Position=0)] $SQLqueryText,
)
$objConnection = New-Object System.Data.SqlClient.SqlConnection;
$objConnection.ConnectionString = $ConnectionString
$objConnection.Open();
$ObjCmd = New-Object System.Data.SqlClient.SqlCommand;
$ObjCmd.CommandText = $SQLqueryText;
$ObjCmd.Connection = $objConnection;
$ObjCmd.CommandTimeout = 0;
$objAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$objAdapter.SelectCommand = $ObjCmd;
$objDataSet = New-Object System.Data.DataSet;
$objAdapter.Fill($objDataSet);
$ResultSets = @();
for ($i=0; $i -lt $objDataSet.Tables.Count; $i++) {
$tmpResultTable = $objDataSet.Tables[$i] | Format-Table | Out-String;
$ResultSets += $tmpResultTable;
}
return $ResultSets
$query = $null;
$objDataSet = $null;
$objConnection.Close();
$objConnection = $null;
}
Get-ChildItem $FolderToSQLFiles -Filter *.sql | Foreach-Object {
$tmpSQLfilePath = $_.FullName
$tmpSQLfileContent = (Get-Content $tmpSQLfilePath) -join "`n"
GetSQLresults -SQLqueryText $tmpSQLfileContent
}
source
share