Powershell Oracle ODBC生成不一致的结果

时间:2016-01-07 23:39:50

标签: oracle powershell odbc

我有一个powershell脚本,它使用12.01.00.02版本的Oracle ODBC驱动程序查询与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客户端运行的同一查询返回的数据不匹配

有人能指出我可能导致问题的原因吗?谢谢。

1 个答案:

答案 0 :(得分:1)

此查询中没有ORDER BY。如果没有ORDER BY,数据库可以按照它关注的任何顺序自由返回行,并且因为正在返回正确的 number 行,我怀疑结果是否存在但是正在返回超出你期望的顺序。如果我正确解释您的问题,您似乎希望按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驱动程序可能已经引入了一个新的令人兴奋的错误。如果上述内容未更改结果,您可以尝试卸载新驱动程序并重新安装旧驱动程序。

祝你好运。