[英]MS Access call SQL Server stored procedure
I have an MS Access application that contains all tables linked to SQL Server, so in MS Access VBA code or query I work with those tables very simple, I access them via name, like [Customers]
. 我有一个MS Access应用程序,其中包含链接到SQL Server的所有表,因此在MS Access VBA代码或查询中,我使用这些表非常简单,我可以通过名称访问它们,例如
[Customers]
。
Also I have a stored procedure in SQL Server called sp_CopyData
which I need to call from my VBA code. 另外,我在SQL Server中有一个名为
sp_CopyData
的存储过程,需要从VBA代码中调用它。 How can I do that without creating new connection to SQL Server (I already have it somewhere!? because I have access to tables)? 在不创建与SQL Server的新连接的情况下该怎么做(我已经在某个地方有了它!因为我可以访问表)?
Or it's impossible? 还是不可能? Appreciate any help.
感谢任何帮助。 Thanks!
谢谢!
The right answer found out, it should be like: 找到正确的答案,应该像这样:
Dim qdef As DAO.QueryDef
Set qdef = CurrentDb.CreateQueryDef("")
qdef.Connect = CurrentDb.TableDefs("[ANY LINKED TABLE TO MS SQL SERVER]").Connect
qdef.SQL = "EXEC sp_CopyData"
qdef.ReturnsRecords = False ''avoid 3065 error
qdef.Execute
Create a pass-though query, and you can then use this throught the WHOLE application anytime you need to execute some T-SQL. 创建一个传递查询,然后您可以在需要执行某些T-SQL的任何时候在整个应用程序中使用此传递查询。
The code this becomes: 代码变为:
With CurrentDb.QueryDefs("qPass")
.SQL = "exec sp_copydata"
.ReturnsRecords = False ''avoid 3065 error
.Execute
End With
The code in MS Access works for me: MS Access中的代码对我有用:
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
cmd.ActiveConnection = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=[DB];Data Source=[PC];Integrated Security=SSPI;"
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "sp_CopyData"
cmd.Parameters.Append cmd.CreateParameter("@param", adVarChar, adParamInput, 255, param)
cmd.Execute
Try: 尝试:
CurrentProject.Connection.Execute "EXEC sp_CopyData"
References: http://msdn.microsoft.com/en-us/library/office/ff821478(v=office.14).aspx 参考: http : //msdn.microsoft.com/en-us/library/office/ff821478(v=office.14).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.