2016-01-07 33 views
0

我有一個使用12.01.00.02版本的Oracle ODBC驅動程序查詢與Oracle數據庫的ODBC連接的PowerShell腳本。Powershell Oracle ODBC生成不一致的結果

我們最近將Oracle服務器升級到版本12.1.0.2.0.1077237,並且從我們看到從以下Powershell腳本返回不一致的結果。

Param (
[parameter(Mandatory = $true)] 
[string]$year, 
[string]$added_days = 0, 
[string]$run_courses = $false, 
[string]$run_enrollments = $false 
) 

$conn = New-Object System.Data.Odbc.OdbcConnection 
$conn.ConnectionString = "dsn=dsnName;PWD=dsnPWD" 
$conn.open() 

$enrollments_query = "select 
    stu.student_number ""Email"", 
    CONCAT(sch.ABBREVIATION, 
     CONCAT(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(cou.COURSE_NAME, '/', '-'), '&', ''), ',', ''), 'ATTENDANCE ', ''), ' ', ''), ':', ''), 
     CONCAT(REPLACE(REPLACE(sec.EXPRESSION, '(', ''), ')', ''), 
      CONCAT(sec.SECTION_NUMBER, 
      CONCAT('-','" + $year + "'))))) as ""Class"" 
    from students stu 
    join cc on stu.ID = cc.STUDENTID 
    join sections sec ON cc.SECTIONID = sec.ID 
    join courses cou ON sec.COURSE_NUMBER = cou.COURSE_NUMBER 
    join schools sch ON sec.SCHOOLID = sch.SCHOOL_NUMBER 
    join teachers tea ON tea.ID = sec.TEACHER 
    where 
    sec.TERMID IN 
     (select ID from SCHEDULETERMS term 
    where CURRENT_DATE BETWEEN term.FIRSTDAY - " + $added_days + " and term.LASTDAY + " + $added_days + " and term.SCHOOLID = sec.SCHOOLID)" 

$enrollments_cmd = New-Object System.Data.Odbc.OdbcCommand($enrollments_query,$conn) 
$enrollments_cmd.CommandTimeout = 300000 
$enrollments_ds = New-Object System.Data.DataSet 
$enrollments_da = New-Object System.Data.Odbc.OdbcDataAdapter($enrollments_cmd) 
$enrollments_da.fill($enrollments_ds) 

$enrollments_ds.Tables[0] | out-gridview 

$enrollments_table = New-Object system.Data.DataTable 
$enrollments_col1 = New-Object system.Data.DataColumn Email,([string]) 
$enrollments_col2 = New-Object system.Data.DataColumn Class,([string]) 
$enrollments_table.columns.add($enrollments_col1) 
$enrollments_table.columns.add($enrollments_col2) 

#loop through each row in database results and add each row to the powershell data table 

foreach ($resRow in $enrollments_ds.Tables[0].Rows) 
{ 
    $row = $enrollments_table.NewRow() 
    $row.Email = $resRow[0] 
    $row.Class = $resRow[1] 
    $enrollments_table.rows.add($row) 
} 

$enrollments_table | export-csv ("C:\windows\temp\td_enrollments.csv") -NoTypeInformation 

它返回行的每一次正確的,但在數據內容是在每次執行不一致,不符合通過ODBC客戶正在運行使用相同的查詢返回的數據。

任何人都可以指向我可能導致問題的原因嗎?謝謝。

+0

你能描述數據是如何不一致的嗎?我們沒有您的數據,表格定義或結果,因此很難猜測。 –

+0

數據返回大約51k行,它是兩列,電子郵件和類名。它應該顯示每個參加每門課程的學生。在查看數據和挑出特定的類名時,應該有來自該類的32個結果(學生)(這是我的ODBC客戶端正確返回的結果)。有時候我會收到4條記錄,有時候是11條,有時候是15條,很少都是32條,但它永遠不會一致,並且大多數情況下總是不包含該特定類名的正確數據記錄數。 – Kris

+1

但你說過要返回的行數是正確的。你看到的行比你想象的多嗎?出門在外,您的查詢沒有'order by'子句,因此不能保證單個類的所有行都將一起返回。是否有可能問題在於,對於分散在結果集中的單個類而言,當您手動查看並非詳盡搜索所有51,000行時,您有「塊」行? –

回答

1

這個查詢沒有ORDER BY。如果沒有ORDER BY,數據庫可以按照它關心的任何順序自由返回行,並且由於正在返回正確的行,我懷疑結果在那裏,但是返回的結果不符合您的預期。如果我正確解釋您的問題,則表示您希望按CLASSEMAIL排序結果;由於CLASS結果的產生是複雜的,我建議你使用字段數排序方法,所以在這種情況下,您的查詢將成爲

$enrollments_query = 
    "select stu.student_number ""Email"", 
      CONCAT(sch.ABBREVIATION, 
      CONCAT(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(cou.COURSE_NAME, '/', '-'), '&', ''), ',', ''), 'ATTENDANCE ', ''), ' ', ''), ':', ''), 
      CONCAT(REPLACE(REPLACE(sec.EXPRESSION, '(', ''), ')', ''), 
      CONCAT(sec.SECTION_NUMBER, 
      CONCAT('-','" + $year + "'))))) as ""Class"" 
    from students stu 
    join cc on stu.ID = cc.STUDENTID 
    join sections sec ON cc.SECTIONID = sec.ID 
    join courses cou ON sec.COURSE_NUMBER = cou.COURSE_NUMBER 
    join schools sch ON sec.SCHOOLID = sch.SCHOOL_NUMBER 
    join teachers tea ON tea.ID = sec.TEACHER 
    where sec.TERMID IN (select ID 
          from SCHEDULETERMS term 
          where CURRENT_DATE BETWEEN term.FIRSTDAY - " + $added_days + 
               " and term.LASTDAY + " + $added_days + " and 
            term.SCHOOLID = sec.SCHOOLID) 
    ORDER BY 2, 1" 

話雖這麼說,這是完全有可能的ODBC的新版本驅動程序可能會引入一個新的令人興奮的bug。如果上述內容不會改變您獲得的結果,請嘗試卸載新驅動程序並重新安裝舊驅動程序。

祝你好運。

+0

在多次執行腳本時,添加order by子句已返回一致數據。 – Kris