如何将数据从Excel发送到SQL临时表以便使用VBA进行处理?

我的团队使用基于Excelmacros的工具来进行一些严肃的数字处理。 我想将数字运算位移动到SQL,因为使用VBA由于运行时间而变得不可容忍。 用户需要能够使用Excel作为接口,并且需要能够同时运行他们的macros,因为Excel工作簿是独立的。 我一直在testing我的计划,从VBA中调用一个SQL存储过程,将数据从Excel中提取到SQL临时表中,将其压缩并发送回Excel。 如果我在SQL Management Studio中运行SP,我可以从Excel中获取数据。 这是SP:

ALTER PROCEDURE sp_test_import -- Add the parameters for the stored procedure here @path varchar(1000) AS BEGIN SET NOCOUNT ON; declare @SQL varchar(2500); set @SQL = ' select * from openrowset(''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0 Macro; Database='+@path+''', [Sheet1$]); ' create table [#test]( col1 varchar(15), col2 varchar(15), col3 varchar(15), col4 varchar(15) ) insert into #test exec(@SQL) select * from #mb_test END 

所以这工作正常。 然后我尝试从包含数据的Excel文件中调用此SP。

 Option Explicit Sub ado_test() Dim adoConnection As ADODB.Connection Dim adoRecordset As ADODB.Recordset Dim connectString As String Dim strSQL As String Dim sPath As String Worksheets("Sheet1").Select sPath = ThisWorkbook.FullName '-Create a new ADO connection -- Set adoConnection = New ADODB.Connection '-Create a new ADO recordset -- Set adoRecordset = New ADODB.Recordset '-Build our connection string to use when we open the connection -- connectString = "DRIVER=SQL Server;SERVER=MyServer;Trusted_Connection=yes;DATABASE=testDB" adoConnection.ConnectionTimeout = 20 adoConnection.CommandTimeout = 20 adoConnection.Open connectString strSQL = "EXEC testDB.dbo.sp_test_import " & vbCr _ & "@path = " & "'" & sPath & "'" adoRecordset.Open strSQL, adoConnection End Sub 

代码挂在“adoRecordset.Open”调用上。 如果我将一个path传递给variables@path中的一个单独的Excel文件,那么一切都会顺利进行。 有一个简单的方法,我可以从同一个工作簿进行这个SP调用? 我不担心安全问题,因为SQL数据库将是一个专门用于提取和处理临时数据的结构。 我只需要用户能够随时运行他们的Excel工具,所以我不想在数据库中使用永久表,以防各自的input混在一起。

我在网上发现的一切与ASP或ISS交易,我对ASP和ISS一无所知似乎不是正确的解决scheme,我的特定问题。 我可以让VBA将数据传递给外部文本文件,然后将这些文本path传递给SQL SP,但如果有更清晰的解决scheme,那么我想知道它。 提前致谢!

我认为这是因为你正在传递一个String数据typesstrSQL作为.Open方法的第一个参数,但是Open方法需要一个Command对象(根据MSDN )。

你要做的是声明一个ADODB.Command对象,并通过它。 我修改了你的代码来做到这一点:

 Option Explicit Sub ado_test() Dim adoConnection As ADODB.Connection Dim adoRecordset As ADODB.Recordset Dim adoCommand As ADODB.Command Dim connectString As String Dim strSQL As String Dim sPath As String Worksheets("Sheet1").Select sPath = ThisWorkbook.FullName '-Create a new ADO connection --' Set adoConnection = New ADODB.Connection '-Create a new ADO recordset --' Set adoRecordset = New ADODB.Recordset '-Create a new ADO command --' Set adoCommand = New ADODB.Command '-Build our connection string to use when we open the connection --' connectString = "DRIVER=SQL Server;SERVER=MyServer;Trusted_Connection=yes;DATABASE=testDB" adoConnection.ConnectionTimeout = 20 adoConnection.CommandTimeout = 20 adoConnection.Open connectString strSQL = "sp_test_import" With adoCommand .ActiveConnection = adoConnection .CommandText = strSQL .CommandType = adCmdStoredProc .Parameters.Refresh .Parameters(1).Value = sPath End With Set adoRecordset = adoCommand.Execute If adoRecordset.EOF = False Then ThisWorkbook.Worksheets("YourSheetName").Cells(1, 1).CopyFromRecordset adoRecordset '--adoRecordset.Open adoCommand, adoConnection' '--Close the database connection' adoConnection.Close End Sub 

有关Command对象的更多信息。

我还添加了如何使用CopyFromRecordset 方法从SQL Server中获取值到Excel工作簿中。