ODBC连接命令文本

时间:2014-10-14 12:18:36

标签: excel vba excel-vba

我在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值,则会出现问题。当它填充命令文本时,我得到一个应用程序定义的或对象定义的错误。

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

对SqlStat的输出进行字符计数,它超过50,000个字符。

我可以使用CommandText填充的最大字符数是多少,是否有任何解决方法?

1 个答案:

答案 0 :(得分:0)

为了将来参考,没有办法绕过极限。

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

VBA:

' 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

SQL:

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))))