訂購

2015-01-20 136 views
0

這裏有一臺,從字面上有我拉我的頭髮:訂購

如果我使用下面的代碼:

SQL = "SELECT * from Incident WHERE" 
SQL = SQL & " active=1 ORDER BY incidentdate" 
Recordset.Open SQL,Connection 
Do While NOT Recordset.Eof 
    idate = FormatDateTime(Recordset("incidentdate"),vbShortDate) 
    inumber = Recordset("incidentnum") 

    Response.Write inumber & "<br />" & vbCrLf 
    Response.Write idate & vbCrLf 
    Recordset.MoveNext  

Loop 

我得到以下輸出:

2015-U1-FO-001 
01/17/2015 

如果我在Do While...行之後切換行,所以我的代碼如下所示:

SQL = "SELECT * from Incident WHERE" 
SQL = SQL & " active=1 ORDER BY incidentdate" 
Recordset.Open SQL,Connection 
Do While NOT Recordset.Eof 
    inumber = Recordset("incidentnum") 
    idate = FormatDateTime(Recordset("incidentdate"),vbShortDate) 

    Response.Write inumber & "<br />" & vbCrLf 
    Response.Write idate & vbCrLf 
    Recordset.MoveNext  

Loop 

我的輸出更改爲:

2015-U1-FO-001 
12/30/1899 

這似乎引起Recordset("incidentdate")返回一個空字符串。

關於我所忽略的任何想法?

+1

入射一個看法? IncidentDate是否有CASE(或COALESCE/ISNULL)?我已經看到類似的邊緣案例根據執行計劃在SQL Server中出現......但我不確定爲什麼執行計劃會在這兩種情況下發生變化。 – 2015-01-20 23:20:05

+1

可以做更多的細節,是'事件'的表或視圖,你可以發佈視圖架構? – Lankymart 2015-01-20 23:34:40

回答

3

我不相信上述的任何事情。

  1. 遊標可能會限制更改的移動,可寫性和可見性,但決不能確定您是否可以(以任何順序)讀取當前記錄的字段。
  2. .Fields集合可以按編號名稱編入索引。在第二種情況下,SELECT語句中字段名的(可能的)順序無關緊要。

證據:

Option Explicit 

Dim sCS : sCS  = "Provider=SQLOLEDB;User ID=...;Password=...;Initial Catalog=Trials;Data Source=WINXPSP3\SQLEXPRESS;" 
Dim oDb : Set oDb = CreateObject("ADODB.Connection") 
oDb.Open sCS 
Dim oRs : Set oRs = CreateObject("ADODB.Recordset") 
oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb 
WScript.Echo oRs.CursorType, "adOpenForwardOnly 0 Default. Uses a forward-only cursor" 
WScript.Echo oRs.GetString(2, , vbTab, vbCrLf, "<Null>") 
oRs.Close 
WScript.Echo "-----------" 
oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb 
WScript.Echo oRs("StartDate") 
WScript.Echo oRs("Id") 
oRs.Close 
WScript.Echo "-----------" 
oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb 
WScript.Echo oRs("Id") 
WScript.Echo oRs("StartDate") 
oRs.Close 
WScript.Echo "-----------" 
oRs.Open "SELECT TOP 3 * FROM [Alpha]", oDb 
WScript.Echo oRs("StartDate") 
WScript.Echo oRs("Id") 
WScript.Echo oRs("Id") 
WScript.Echo oRs("StartDate") 
oRs.MoveNext 
WScript.Echo "..........." 
WScript.Echo oRs("StartDate") 
WScript.Echo oRs("Id") 
oRs.MoveNext 
WScript.Echo "..........." 
WScript.Echo oRs("Id") 
WScript.Echo oRs("StartDate") 
oRs.Close 
oDb.Close 

輸出:

cscript 28054592.vbs 
0 adOpenForwardOnly 0 Default. Uses a forward-only cursor 
209  10.06.2002  11.02.2003  2090 
418  28.04.2000  10.06.2000  4180 
627  03.09.2000  01.11.2000  6270 

----------- 
10.06.2002 
209 
----------- 
209 
10.06.2002 
----------- 
10.06.2002 
209 
209 
10.06.2002 
........... 
28.04.2000 
418 
........... 
627 
03.09.2000 
+0

如果我是誠實的,我有同樣的觀點。 – Lankymart 2015-01-20 23:32:08