我有一个SQL查询,可以成功在SSMS中运行。但是确实需要我连接到两个不同的来源。我已将来源命名为一个简单的名称,以便不共享公司数据。
SELECT TOP (1000)
tbl1.[PartNumber],
tbl1.[PartDescription],
tbl1.[PartDescriptionLong],
tbl1.[PhaseNumber],
tbl2.FULL_NAME
FROM
[hidden1].[hid1].[table1Name] AS tbl1
LEFT JOIN
[hidden2].[hid2].[table2Name] as tbl2
ON
tbl2.USER_NAME=tbl1.ProductManager
在其他情况下,我已经能够在Excel VBA中使用以下代码连接到数据源,然后执行查询,然后填充表。
Dim wb As Workbook: Set wb = Application.Workbooks.Add
Dim ws As Worksheet: Set ws = wb.Sheets(1)
Dim wkStation As String: wkStation = VBA.Environ("computername")
With ws.ListObjects.Add( _
SourceType:=xlSrcExternal, _
Source:=Array("OLEDB;", _
"Provider=SQLOLEDB.1;", _
"Integrated Security=SSPI;", _
"Persist Security Info=True;", _
"Data Source=SourceAddress;", _
"Use Procedure for Prepare=1;", _
"Auto Translate=True;", _
"Packet Size=4096;", _
"Workstation ID=" & wkStation & ";", _
"Use Encryption for Data=False;", _
"Tag with column collation when possible=False;", _
"Initial Catalog=catalogName"), _
Destination:=ws.Range("A1")).QueryTable
.CommandType = xlCmdSql
.CommandText = sql
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
第十行是这个"Data Source=SourceAddress;"
,当我只有1个数据源时,这很好用。但是当我有2个时,我不知道该怎么办。任何帮助将不胜感激。