如何在Excel中无法以图形方式显示的外部数据查询中添加参数?
我经常使用 MS Excel 的获取外部数据
来创建简单的报告 - 对数据库运行查询并在 Excel 中很好地显示。 Excel 的强大功能(例如过滤和数据透视表)以及用户熟悉的界面使其非常适合此目的。但是,Microsoft Query 的一个限制是您无法向无法以图形方式显示的查询添加参数,这极大地限制了您可以编写的 SQL。
对于“无法以图形方式显示的查询中不允许使用参数”错误有什么解决方案吗?
I often use MS Excel's Get External Data
to create simple reports - running queries against databases and displaying nicely in Excel. Excel's great features like filtering and pivot tables and familiar interface for users make it quite good for this. However, one limitation with Microsoft Query is you can't add parameters to queries that can't be displayed graphically, which considerably limits the SQL you can write.
Is there any solution to the error "parameters are not allowed in queries that can't be displayed graphically"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用于 SQL Server 查询的 Excel 界面不允许您拥有自定义参数。解决此问题的方法是创建通用 Microsoft Query,然后添加参数,然后将参数化查询粘贴到连接的属性中。以下是 Excel 2010 的详细步骤:
WHERE 1 = ?和 2 = ?
Excel's interface for SQL Server queries will not let you have a custom parameters. A way around this is to create a generic Microsoft Query, then add parameters, then paste your parametorized query in the connection's properties. Here are the detailed steps for Excel 2010:
WHERE 1 = ? and 2 = ?
简单的解决方法(无需 VBA)
从此处直接编辑 SQL通过添加“?”无论你想要一个参数。工作方式与以前相同,只是您不会被打扰。
Easy Workaround (no VBA required)
From here, edit the SQL directly by adding '?' wherever you want a parameter. Works the same way as before except you don't get nagged.
如果您有 Excel 2007,则可以编写 VBA 来更改工作簿中的连接(即外部数据查询)并更新 CommandText 属性。如果您只需在需要参数的位置添加
?
,那么下次您刷新数据时,它会提示您输入连接的值!魔法。当您查看连接的属性时,参数按钮现在将处于活动状态并且可以正常使用。例如,我会编写一个宏,在调试器中单步执行它,并使其适当地设置 CommandText。完成此操作后,您可以删除宏 - 它只是更新查询的一种方法。
If you have Excel 2007 you can write VBA to alter the connections (i.e. the external data queries) in a workbook and update the CommandText property. If you simply add
?
where you want a parameter, then next time you refresh the data it'll prompt for the values for the connections! magic. When you look at the properties of the Connection the Parameters button will now be active and useable as normal.E.g. I'd write a macro, step through it in the debugger, and make it set the CommandText appropriately. Once you've done this you can remove the macro - it's just a means to update the query.
对于 Excel 2013,即使查询文本包含“?”等参数,连接对话框中的“参数”按钮仍保持禁用状态。
将参数插入查询文本,如下所示:
在 VBA 中添加:
For Excel 2013 button "Parameters", in connection dialog, remains disabled even if the query text contains parameters like "?".
Insert parameters into query text like this:
In VBA add:
是 - 解决方案是将工作簿保存到 XML 文件(例如“XML Spreadsheet 2003”)并在记事本中将此文件编辑为文本!
使用记事本的“搜索”功能查找查询文本并将数据更改为“?”。
保存并在 Excel 中打开,尝试刷新数据,Excel 将监视参数。
YES - solution is to save workbook in to XML file (eg. 'XML Spreadsheet 2003') and edit this file as text in notepad!
use "SEARCH" function of notepad to find query text and change your data to "?".
save and open in excel, try refresh data and excel will be monit about parameters.