2012-07-19 1 views
2

이전에 이런 질문이 있었지만 여기 또는 인터넷에서 아무것도 찾을 수 없습니다. PowerShell을 사용하여 oracle 데이터베이스를 쿼리하려고하는데, null 값에 도달 할 때까지 확인한 다음 불면.powershell에서 nulls 처리하기

여기 내 코드입니다.

# 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 " 
} 

오류 메시지 : "1"인수 (들) "하여 GetString"를 호출

예외 : C에서 "열은 NULL 데이터가 포함 된": \ 사용자 \ klynch \ test.ps1 : 34 숯 27 + $ DNP = $ reader.GetString < < < < (16) + CategoryInfo : NotSpecified (:) [] MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

+0

"그러면 폭발합니다."- 오류 메시지를 보내 주시겠습니까? – Ollie

+0

위 오류 메시지가 추가되었습니다. – Ken

답변

1

"SELECT *"를 제거하고 명시 적으로 필드의 이름을 지정합니다. 그런 다음 NVL/COALESCE를 사용하여 NULL 값을 처리 할 수있는 것으로 변환 할 수 있습니다. 앞에 입력하는 것이 더 많지만, 테이블 구조가 변경 될 경우 나중에 선택하는 것을 문서화하고 나중에 오류를 피할 수 있습니다.

SELECT *

은 임시 형 쿼리하지만 아무것도 IMO 나쁜 관행이다. 현재 행

if ($reader.IsDBNUll(column)) { 
    # Handle null 
} else { 
    # Get the value 
} 
+0

당신은 나쁜 행동 인 선택 *에 대해 맞습니다. 나는 자바 프로젝트를 powershell로 변환하여 사용했던 쿼리를 복사하여 붙여 넣었다. 고맙습니다. 나는 약간의 조정을하고 어떤 일이 일어날지를 볼 것입니다. – Ken

+0

Ok NVL은 항상 null 인 열에 대해서는 정상적으로 작동하지만 null 값이 몇 개 밖에없는 열에 대해서는 작동하지 않으며 병합하면 null 값을 가진 레코드를 건너 뛸 수 있습니다. – Ken

+0

"작동하지 않았다"는 것은 무엇을 의미합니까? – DCookie

1

확인해 볼 수 있습니다. 이 객체는 null 값을 저장할 수 있으므로 null을 확인하는 IsNull 속성과 Value 속성을 가질 수 있으며 가능하면 변환 메소드도 사용할 수 있습니다. ToInt32, 일반 .NET 유형의 값을 가져옵니다.

+0

그것은 내가 감사해야 할 마지막 작품이었습니다. – Ken

0

다른 해결책은 Oracle Data Provider 오브젝트를 리턴 할 reader.GetOracleValue 방법을 사용하는 것이다 함께 주어진 컬럼에 대한 널이 있는지