作者:瑞风大姑娘_214 | 来源:互联网 | 2023-07-24 12:20
我创建了一个基本的Sub来运行查询,并在单元格A1中的活动工作表中返回结果。
Sub MySub()
'Assemble the connection string
strDsn = "MyDsn"
strPass = "MyPass"
strusername = "MyUser"
strCOnnection= "ODBC;Dsn=" & strDsn & ";UID=" & strusername & ";PWD=" & strPass
strQuery = "MyQuery"
With activeSheet.QueryTables.Add(Connection:=strConnection,Destination:=Cells(1,1))
.CommandText = strQuery
.FieldNames = True
.RefreshStyle = 0
.AdjustColumnWidth = True
.Refresh BackgroundQuery:=False
End With
End Sub
此子项可与以下查询配合使用:
Select distinct TOP 3 Time FROM [WBServiceLogs]
如果我在SQL中声明一个变量,例如,sub将失败:
Declare @Shift INT SET @Shift = 100 Select distinct TOP 3 Time + @Shift FROM [WBServiceLogs]
我收到一个错误:Application-defined or object-defined error
查询本身是可以的,它在SQL Server Mgt Studio中运行良好。或例如通过ODBC WinSQL。
有什么建议吗?谢谢!