我知道这是一个较老的问题,但我试图尝试同样的努力,而当我寻找例子时,我通常看到了“不可能”的相同答案。
正如Thomas G指出的那样,它可能是一个'断开的记录集'(我通常将这些记录称为子查询),然后将两个列表连接在一起。
我的目标是避免需要首先将数据导入到Excel中,只是为了运行带有已加入列表的查询,因为我实际上使用它将每周批量导入多个文件到SharePoint中的多个列表中。我只是不想处理Excel中的数据维护/清理工作,如果我能够避免它的话。 (这几个文件都是在导入过程中使用来自两个不同列表的数据)
与原始问题一致,我简化了代码以仅检索两个SharePoint列表的Select查询结果。该代码是为Excel VBA编写的,但如果删除了Excel部分,则可以在任何VBA实例中使用该代码。
Sub SQL_Two_SP_Lists()
Dim sp_sdbPath As String, sp_sConnect As String
Dim SP_List_1 As String, SP_List_2 As String
Dim c As Long
Dim cnSP As New ADODB.Connection
Dim rsSP As New ADODB.Recordset
sp_sdbPath = "https://your_SharePoint_URL_Here/"
sp_sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes;DATABASE=" & sp_sdbPath & ";"
SP_List_1 = "LIST={List 1 GUID Here}" 'i.e. xxxx-guid-xxxx
SP_List_2 = "LIST={List 2 GUID Here}" 'Go to List settings > Information Management Policy Settings > GUID is in the URL
'Establish a connection to the first List
cnSP = sp_sConnect + SP_List_1
cnSP.Open
'Write the SQL & Establish a connection to the second List as a sub-query using IN
sSQL = "SELECT A.*, B.* " + _
"FROM List A " + _
"INNER JOIN (Select * From LIST IN 'DATABASE=" & sp_sdbPath & ";" & SP_List_2 & "' 'WSS;RetrieveIds=Yes;') B On A.Cust_ID = B.Cust_ID;"
rsSP.Open sSQL, cnSP, adOpenStatic, adLockReadOnly 'Change cursor & lock type if inserting, updating or deleting
'The rest is to drop the results into an empty worksheet named 'Test'
For c = 0 To rsSP.Fields.Count - 1
ThisWorkbook.Sheets("Test").Cells(1, c + 1) = rsSP.Fields(c).Name
Next
ThisWorkbook.Sheets("Test").Cells(2, 1).CopyFromRecordset rsSP
rsSP.Close
cnSP.Close
末次
我environement是SharePoint网站与许多名单,我想从Excel中查询值,以创建具有价值的图形。我确定如果我使用for循环进行内部连接,这将会非常缓慢: – Steeve 2014-10-31 08:09:38
好吧我认为这是为了Access,所以忘记了连接的表格,因此应该打开2个不同的记录集并循环遍历它们。慢,尝试首先导入每个记录集在单独的Excel表(应该是快速的批量模式)和循环的工作表的值。这就是我想采取 – 2014-10-31 08:16:05
感谢您的答案,有可能对待工作表像表和使用SQL在两者之间进行查询? – Steeve 2014-10-31 08:23:01