从 Excel 2003/MS Query 调用 SQL Server 2000 内联表值函数

发布于 2024-08-08 01:00:29 字数 2120 浏览 9 评论 0原文

我们的很大一部分用户群通过使用 Microsoft Query 在 Excel 2003 内构建 ODBC 查询来访问公司数据。对于更复杂的事情,他们经常让我参与其中。

在很多情况下,我决定提取某些数据的最合理的方法是使用内联表值函数来实现参数化视图的功能。从查询分析器调用时效果很好:

SELECT * FROM fn_AverageRecovery('2009-07','2009-10')

Sequence    Process Centre                                     Process Centres_Description                        Input Qty                                             Output Qty                                            Recovery                                              
----------- -------------------------------------------------- -------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- 
10          GM                                                 Green Mill                                         12345.678                                             11223.344                                             11
11          LYR                                                Log Yard Report                                    98765.432                                             55443.322                                             99   
20          MB                                                 MultiBand Resaw                                    5555.666                                              5555.444                                              50                

但是在 MS Query 中输入相同的 SELECT 子句会导致错误:无法添加表 'fn_AverageRecovery('2009-07''。

不仅如此,我真正想做的是从电子表格中获取的周期参数,但是如果我用问号替换文字,MS Query 就会出现可怕的错误:无法以图形方式显示的查询中不允许使用参数

现在,我有时设法避免这种情况 。过去,通过使用相当不优雅的方法输入一些简单的 SQL 语句,单击 Excel 中结果集中的任何单元格,然后切换到 VBA 并在立即窗口中手动设置 CommandText 属性:

ActiveCell.QueryTable.CommandText="select * from fn_AverageRecovery(?,?)"

VBA 没有抱怨但是当我切换时。返回 Excel 并右键单击单元格并选择“刷新数据” 我收到两个错误:

[Microsoft][ODBC SQL Server Driver]无效的参数号

[Microsoft][ODBC SQL Server Driver]无效的描述符索引

每次我都会遇到我花了一个小时左右试图让它工作,但总是不得不诉诸其他方式解决它。我很想知道是否有人征服了这个。

谢谢韦恩

·象牙

A large part of our user base accesses corporate data by building ODBC queries inside Excel 2003 using Microsoft Query. For the more complex stuff they often get me involved.

There have been a number of occasions whereby I've decided that the most logical way to extract certain data would be to use an Inline Table-Valued Function to achieve the functionality of a parameterised view. This works fine when called from Query Analyzer:

SELECT * FROM fn_AverageRecovery('2009-07','2009-10')

Sequence    Process Centre                                     Process Centres_Description                        Input Qty                                             Output Qty                                            Recovery                                              
----------- -------------------------------------------------- -------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- ----------------------------------------------------- 
10          GM                                                 Green Mill                                         12345.678                                             11223.344                                             11
11          LYR                                                Log Yard Report                                    98765.432                                             55443.322                                             99   
20          MB                                                 MultiBand Resaw                                    5555.666                                              5555.444                                              50                

However entering the same SELECT clause in MS Query causes the error: Could not add the table 'fn_AverageRecovery('2009-07''.

Not only that, but what I really want to do is have the Period parameters picked up from the spreadsheet, however if I replace the literals with question marks MS Query gives me the dreaded error: Parameters are not allowed in queries that can't be displayed graphically.

Now, I have managed to circumvent this situation sometimes in the past by using the rather inelegant method of entering some simple SQL statement, clicking any cell in the result set within Excel, then switching to VBA and setting the CommandText property manually in the Immediate window:

ActiveCell.QueryTable.CommandText="select * from fn_AverageRecovery(?,?)"

No complaints from VBA. But when I switch back to Excel and right-click on the cell and choose Refresh Data I get two errors:

[Microsoft][ODBC SQL Server Driver]Invalid parameter number

[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index

Every time I've come across this I've spent an hour or so trying to get it working but have always had to resort to solving it another way. I'd love to know if anybody has conquered this.

Thanks

Wayne Ivory

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最后的乘客 2024-08-15 01:00:29

您可以尝试将其放入存储过程中,然后让 Excel 工作表运行该存储过程。我认为它能够处理这个问题。然后只需让存储过程针对表函数运行查询即可。

You could try putting this into a stored procedure, and having the Excel sheet run the stored procedure. I would think it would be able to handle this. Then simply have the stored procedure run the query against the table function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文