如何在 Microsoft Excel 中执行存储的 SQL 过程?

Saf*_*Ali 4 vba sql-server-2008 data-transfer microsoft-excel

如何在 Microsoft Excel 中执行存储过程并获取它返回的所有数据?

Exc*_*lll 5

这是 VBA 中 ADODB 连接的工作。这是一个包含简单 SELECT 查询示例代码的链接,但这也将处理存储过程。

http://www.ozgrid.com/forum/showthread.php?t=83016&page=1

关键项目申报的需要ADODB.ConnectionADODB.Recordset和匹配您的数据库连接字符串。打开连接后,您可以使用如下语法(取自链接)执行 SQL 语句:

With cnt 
    .CursorLocation = adUseClient 
    .Open stADO    // stADO is the connection string.
    .CommandTimeout = 0 
    Set rst = .Execute(stSQL) 
End With
Run Code Online (Sandbox Code Playgroud)

然后rst使用Range.CopyFromRecordSet.