VBA中的SQL语句过长

时间:2019-07-13 14:54:51

标签: sql vba

为了使完​​整的SQL我将其存储在工作表之一中,然后从那里提取,但是如果语句太长而导致错误,因为vba无法捕获完整的SQL代码(如果过滤器数据不是很多)它工作正常)。我是在VBA中使用SQL的新手,希望有什么办法可以解决这个问题

请参考下面我创建的代码,谢谢。当请求太多数据时,它将在Commandtext = Array(SQLCODE)处停止。谢谢

CODE1 = Worksheets("Sheet1").Range("A6")

SQLCODE = CODE1
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DSN=EBT-RAW;UID=00174;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=SNG-LP-0153;DATABASE=EBTRawConsumption" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array(SQLCODE)
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_EBT_RAW"
        .Refresh BackgroundQuery:=False
    End With

这是SQLCODE

Select * FROM EBTRawConsumption.dbo.AMI_UsageData 
WHERE UsageDate >= '2019-6-1'and UsageDate <= '2019-7-1'
and MeterID in ('','000572992','000674117','000685835','000680646','000681342','000697973','000697890','000728193','000728213','000730270','000730266','000730800','000730267','000732442','000731276','000730869','000731890','000731592','000735403','000737161','000737976','000739532','000738001','000737581','000737596','000737996','000738298','000737587','000738840','000739370','000739367','000736241','000737089','000738970','000740763','000742172','000746989','000750232','000749300','000749303','000749179')

0 个答案:

没有答案