I’m sure this has been asked before but I couldn’t find anything on here or on the Internet. I’m trying to use powershell to query an oracle database which it seems to do ok until it hits a null value and then it blows up.
Here is my code.
# Create a datareader for a SQL statement
$sql="select * from legacydb.ebt_invoice_dtl where premnum = 397743"
$command = New-Object Oracle.DataAccess.Client.OracleCommand( $sql,$conn)
$reader=$command.ExecuteReader()
# Write out the result set structure
for ($i=0;$i -lt $reader.FieldCount;$i++) {
Write-Host $reader.GetName($i) $reader.GetDataTypeName($i)
}
# Write out the results
while ($reader.read()) {
$dtl_id=$reader.GetDecimal(0)
$invoice_id=$reader.GetDecimal(1)
$debtor=$reader.GetInt64(2)
$premise=$reader.GetInt64(3)
$license=$reader.GetString(4)
$invoice_num=$reader.GetInt64(5)
$deb_cred=$reader.GetString(6)
$inv_ref=$reader.GetString(7)
$rate=$reader.GetDecimal(8)
$charge=$reader.GetDouble(9)
$usage=$reader.GetInt64(10)
$tax=$reader.GetDouble(11)
$rate_code=$reader.GetString(12)
$inv_date=$reader.GetDateTime(13)
$missed=$reader.GetString(14)
$change_date=$reader.GetDateTime(15)
$dnp=$reader.GetString(16)
Write-Host "$dtl_id $invoice_id $debtor $premise $license $invoice_num $deb_cred $inv_ref $rate $charge $usage $tax $rate_code $inv_date $missed $change_date $dnp "
}
Error Message:
Exception calling “GetString” with “1” argument(s): “Column contains
NULL data” At C:\users\klynch\test.ps1:34 char:27
+ $dnp=$reader.GetString <<<< (16)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
I would get rid of the “SELECT *” and explicitly name your fields. Then you could use NVL/COALESCE to convert your NULL values into something you could handle. It’s more typing up front, but it documents what you’re selecting and might help you avoid errors later if the table structure changes.
is bad practice, IMO, for anything but ad-hoc type queries.