gpt4 book ai didi

sql - 带有 SQL : No row returned when Where clause specified 的 Excel VBA

转载 作者:行者123 更新时间:2023-12-04 22:30:06 25 4
gpt4 key购买 nike

下面是我的代码的摘录。

这是 SQL 命令:

   vSQL = "SELECT OLIN.fk_cCUST as CustomerKey, Sum(nOLINselTota) AS ResultSumV "
vSQL = vSQL & " FROM (OLIN Inner Join TYOR on OLIN.fk_cTYOR = TYOR.cTYOR) INNER Join MTYP On TYOR.fk_cMTYP = MTYP.cMTYP "
vSQL = vSQL & " Where OLIN.fk_cOHEAkey Like 'T180*' "
vSQL = vSQL & " GROUP BY OLIN.fk_cCUST "
vSQL = vSQL & ";"

数据库所在的位置:

   vDataSRC = "C:\_projCuTOPs\bdd\GSF_dataWHouse.accdb"

包含与 DB 的字符串连接的变量:

   vArrSRC = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;"
vArrSRC = vArrSRC & "Data Source= " & vDataSRC & ";"
vArrSRC = vArrSRC & "Mode=Share Deny Write;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";"
vArrSRC = vArrSRC & "Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;"
vArrSRC = vArrSRC & "Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;"
vArrSRC = vArrSRC & "Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;"
vArrSRC = vArrSRC & "Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False"

结果的 wrkSheet :

  ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveWorkbook.Sheets(Worksheets.Count).Name = "RawDatas"

由于 Where 子句,此命令将 QueryTable 添加为 ListObject 返回零次出现,而 SQL 则正常。

如果删除 Where 子句,它可以工作,但我需要添加对行的限制。

With ActiveSheet.ListObjects.Add(SourceType:=xlSrcExternal, Source:=vArrSRC, Destination:=Range("$A$1")).QueryTable
'>>
.CommandType = xlCmdSql
.CommandText = vSQL
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
'**
.SourceDataFile = vDataSRC
'**
.ListObject.DisplayName = "tbl_SQL_SumTYOR"
.Refresh BackgroundQuery:=False
'>>
End With

怎么了 ?我绝对需要使用 Where 子句。

最佳答案

LIKE在 MS Access GUI(前端)和与 MS Access(后端)的任何 ODBC/OLDEB 连接之间运行查询时,通配符的行为不同。参见 MSDN docs 中 ANSI-89 和 ANSI-92 之间的区别.

对于您在 Excel 中所做的 ODBC/OLEDB 连接,LIKE需要带有 % 的 ANSI-92 通配符:

vSQL = vSQL & " Where OLIN.fk_cOHEAkey Like 'T180%' "

或者,要兼容两者,请使用 ALIKE GUI 和 ODBC/OLEDB 中的(类 ANSI):
vSQL = vSQL & " Where OLIN.fk_cOHEAkey ALike 'T180%' "

更好的是,将查询保存在 MS Access 中(由于引擎缓存统计信息和最佳执行计划,效率更高):

SELECT OLIN.fk_cCUST as CustomerKey, 
SUM(nOLINselTota) AS ResultSumV
FROM (OLIN INNER JOIN TYOR ON OLIN.fk_cTYOR = TYOR.cTYOR)
INNER Join MTYP On TYOR.fk_cMTYP = MTYP.cMTYP
WHERE OLIN.fk_cOHEAkey LIKE 'T180*'
GROUP BY OLIN.fk_cCUST

然后,在 Excel 中引用其名称(避免 VBA 连接):
vSQL = "SELECT * FROM mySavedQuery"

关于sql - 带有 SQL : No row returned when Where clause specified 的 Excel VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53855368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com