VBScript Excel ADO 连接:在 SQL 查询中使用列别名获取值

VBScript Excel ADO connection: Get value using column alias in SQL query

在下面的代码中,我可以使用

从 excel 中获取计数

objTempRecordset.Fields.Item(0).Value

但是,我想在 SQL 中使用列名别名。 即

sSQL = "Select Count(*) AS RecCount FROM [NELimits$] A WHERE A.Type = 'A' AND A.ID = " &Chr(39) & "R001" & Chr(39)

我想使用以下方法得到结果:

objTempRecordset.Fields.Item("RecCount").Value

我也试过objTempRecordset.Fields.Item("_Count(*)_").Value但没有成功

谁能告诉我在这种情况下如何使用列名别名?

注意:Excel 有 2 列 ID:具有"R001"、"R002"等值 类型:具有 "A"、"B"、"C"

等值

示例代码:

sSQL = "Select Count(*) FROM [NELimits$] A WHERE A.Type = 'A' AND A.ID = " &Chr(39) & "R003" & Chr(39)
Sqlquery = sSQL 
sFilePath = "C:\Temp\DataSheet.xlsx"

Dim objTempConnection : Set objTempConnection = CreateObject("ADODB.Connection")
Dim objTempRecordSet : Set objTempRecordSet = CreateObject("ADODB.Recordset")

Dim strPath
'Define constants for objTempRecordset
Const adOpenStatic=3
Const adLockOptimistic=3
Const adLockPessimistic=2
Const adCmdText = &H001

'Open connection 
objTempConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="& sFilePath &";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

objTempRecordset.ActiveConnection = objTempConnection
objTempRecordset.CursorType = adOpenStatic
objTempRecordset.LockType = adLockOptimistic 
objTempRecordset.Open Sqlquery 

If objTempRecordset.EOF Or objTempRecordset.BOF Then
    msgbox "no record"
End If

msgbox "Record Count: "&objTempRecordset.RecordCount

msgbox "Value:" & objTempRecordset.Fields.Item(0).Value

使用 ACE SQL 引擎(此处用于查询工作簿)、原始字段名称、列别名或 table 名称中包含空格、特殊字符(非字母数字)或 reserved words 需要包含在 方括号 反引号 中以正确转义它们。

空间

sSQL = "Select Count(*) AS [Rec Count] FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"
sSQL = "Select Count(*) AS `Rec Count` FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"

特殊字符(例如,连字符和 pound/hashtag 符号)

sSQL = "Select Count(*) AS [Rec-Count] FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"
sSQL = "Select Count(*) AS `Rec-Count` FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"

sSQL = "Select Count(*) AS [Rec#] FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"
sSQL = "Select Count(*) AS `Rec#` FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"

保留字(例如,计数)

sSQL = "Select Count(*) AS [Count] FROM [NELimits$] A" _
           & "  WHERE A.Type = 'A' AND A.ID = 'R003'"
sSQL = "Select Count(*) AS `Count` FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"

否则,任何字段名或列别名在查询中都是合法的,可以按以下格式在记录集中读取:

objTempRecordset.Fields.Item(0).Value               ' BY INDEX IN ITEM '
objTempRecordset.Fields.Item("Rec Count").Value     ' BY NAME IN ITEM '
objTempRecordset.Fields("Rec Count").Value          ' BY NAME IN FIELD COLLECTION '
objTempRecordset![Rec Count].Value                  ' BY NAME (EXCLAMATION POINT QUALIFIER) '

此外,ACE 以特殊方式处理缺失的列别名:

查询表达式缺少别名(例如,计数函数聚合)

sSQL = "Select Count(*) FROM [NELimits$] A" _ 
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"

重复字段缺少别名

sSQL = "Select ID, ID FROM [NELimits$] A" _
           & " WHERE A.Type = 'A' AND A.ID = 'R003'"

对于以上两个缺失的别名,ACE 引擎创建一个列别名,通常从 Expr1(在 MS Access 中 - ACE 引擎的常用接口)或 Expr1000 用于 ODBC 连接并递增所有其他未命名的表达式或未命名的重复字段引用。