Vba ODBC连接命令文本

Vba ODBC连接命令文本,vba,excel,Vba,Excel,我在VBA代码中多次成功地使用下面的语句 With ActiveWorkbook.Connections("Query").ODBCConnection .CommandText = SqlStat .Refresh End With 我正在根据输入Excel并插入查询其余部分的参数填充SQLStat 其中一个参数来自Excel中的列范围。我有下面的代码在这个范围内循环,并将其中的每个值放入一个字符串中,用逗号分隔。 然后我在查询的IN语句中使用这个字符串,这样它只返回我想要的

我在VBA代码中多次成功地使用下面的语句

With ActiveWorkbook.Connections("Query").ODBCConnection
    .CommandText = SqlStat
    .Refresh
End With
我正在根据输入Excel并插入查询其余部分的参数填充SQLStat

其中一个参数来自Excel中的列范围。我有下面的代码在这个范围内循环,并将其中的每个值放入一个字符串中,用逗号分隔。 然后我在查询的IN语句中使用这个字符串,这样它只返回我想要的结果

' Gets List of Cust-SPA-Var
i = 3
' Gets column number
col_n = Sheets("Data").Range("N2").Value
'Determine the last row with data in column on Rebate List worksheet
lastRow = Sheets("Rebate Lists").Cells(Rows.Count, col_n).End(xlUp).Row

'Fills Down lookup Formula
Sheets("Rebate Lists").Select
    Sheets("Rebate Lists").Cells(3, col_n).Select
Selection.AutoFill Destination:=Range("tblCustSPAVar[Cust-SPA-Var]"), Type:=xlFillDefault

' Loops through each row, checks if it exists in the string and if not, appends it to the end.
Do Until i = (lastRow + 1)
    If InStr(CustSPAVar, Sheets("Rebate Lists").Cells(i, col_n).Value) Then
        Else: CustSPAVar = CustSPAVar & "'" & Sheets("Rebate Lists").Cells(i, col_n).Value & "', "
    End If
    i = i + 1
Loop
如果在运行代码时有很多CustSPAVar值,则会出现问题。在填充命令文本时,我得到一个应用程序定义或对象定义的错误

如果我debug.printSqlStat然后手动将SQL代码复制出即时窗口,则SQL代码是正确的。我甚至可以手动更新查询以使用复制的SQL,它接受并返回我期望的结果

对SqlStat的输出进行字符计数时,它将超过50000个字符


我可以填充CommandText的最大字符数是多少?有什么办法吗?

为了以后的参考,没有办法绕过这个限制

我必须动态地将参数插入到一个临时CSV文件中,然后使用OPENROWSET在查询中引用它

VBA:

SQL:


-querytable.commandtext允许的最大长度为32767个字符。如果您试图分配一个较大的字符串,将触发一个对象定义的错误。是否有任何可能解决此限制的方法?通常我会在我的表中使用别名,但语句的select部分很小,它是文本的主要部分,显然我不能减少它们。我不知道有什么方法可以超出限制。你能把这个查询分成几个查询吗。i、 e.将第一个查询存储到临时表中,然后进一步查询临时表?该查询实际上是2个联合在一起的。我想我可以将它们分开,然后在以后的代码中合并结果集,但在将来,参数列表的长度可能是当前长度的2倍,因此仍然无法解决这个问题,因为我怀疑每个查询很容易超过32000个字符。
' Saves Rebate Variants as CSV file in Temp directory.
Sheets("Rebate Variants").Select
Sheets("Rebate Variants").Copy
ActiveWorkbook.SaveAs Filename:=TempPath & "\Variants.csv", _
    FileFormat:=xlCSV, CreateBackup:=False
ActiveWindow.Close

If Len(Dir(TempPath & "\Variants.csv", vbDirectory)) = 0 Then
    MsgBox "Error creating " & TempPath & "\Variants.csv"
    Exit Sub
End If






' Creates the XML files definition
XmlText = _
"<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?> " & Chr(13) & _
"<BCPFORMAT xmlns=" & Chr(34) & "http://schemas.microsoft.com/sqlserver/2004/bulkload/format" & Chr(34) & " xmlns:xsi=" & Chr(34) & "http://www.w3.org/2001/XMLSchema-instance" & Chr(34) & "> " & Chr(13) & _
 "<RECORD> " & Chr(13) & _
  "<FIELD ID=" & Chr(34) & "1" & Chr(34) & " xsi:type=" & Chr(34) & "CharTerm" & Chr(34) & " TERMINATOR=" & Chr(34) & "," & Chr(34) & " MAX_LENGTH=" & Chr(34) & "510" & Chr(34) & " COLLATION=" & Chr(34) & "SQL_Latin1_General_CP1_CI_AS" & Chr(34) & "/> " & Chr(13) & _
  "<FIELD ID=" & Chr(34) & "2" & Chr(34) & " xsi:type=" & Chr(34) & "CharTerm" & Chr(34) & " TERMINATOR=" & Chr(34) & "," & Chr(34) & " MAX_LENGTH=" & Chr(34) & "510" & Chr(34) & " COLLATION=" & Chr(34) & "SQL_Latin1_General_CP1_CI_AS" & Chr(34) & "/> " & Chr(13) & _
    "<FIELD ID=" & Chr(34) & "3" & Chr(34) & " xsi:type=" & Chr(34) & "CharTerm" & Chr(34) & " TERMINATOR=" & Chr(34) & "\r\n" & Chr(34) & " MAX_LENGTH=" & Chr(34) & "510" & Chr(34) & " COLLATION=" & Chr(34) & "SQL_Latin1_General_CP1_CI_AS" & Chr(34) & "/> " & Chr(13) & _
 "</RECORD> " & Chr(13) & _
 "<ROW> " & Chr(13) & _
  "<COLUMN SOURCE=" & Chr(34) & "1" & Chr(34) & " NAME=" & Chr(34) & "SPA" & Chr(34) & " xsi:type=" & Chr(34) & "SQLNVARCHAR" & Chr(34) & "/> " & Chr(13) & _
  "<COLUMN SOURCE=" & Chr(34) & "2" & Chr(34) & " NAME=" & Chr(34) & "VariantCode" & Chr(34) & " xsi:type=" & Chr(34) & "SQLNVARCHAR" & Chr(34) & "/> " & Chr(13) & _
  "<COLUMN SOURCE=" & Chr(34) & "3" & Chr(34) & " NAME=" & Chr(34) & "Rebate" & Chr(34) & " xsi:type=" & Chr(34) & "SQLNVARCHAR" & Chr(34) & "/> " & Chr(13) & _
 "</ROW> " & Chr(13) & _
"</BCPFORMAT> "

' Creates Variant XML File
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(TempPath & "\VariantsFormat.xml", True)
a.WriteLine (XmlText)
a.Close
CustVarParam as (SELECT  a.SPA+'-'+a.VariantCode as CustVar, cast(a.Rebate as Decimal(10,2)) as RebateValue   FROM    OPENROWSET(BULK 'U:\temp\Variants.csv', FORMATFILE= 'U:\temp\VariantsFormat.xml') AS a where a.SPA <> 'SPA') 

Select XXX
From XX
Where variant_detail.vad_variant_code In  ((Select CustVarParam.CustVar from CustVarParam))))