为什么在本地服务器上使用 OPENQUERY 不好?
我正在编写一个脚本,该脚本应该在一堆服务器上运行并从中选择一堆数据,包括本地服务器。选择我需要的数据所需的 SQL 非常复杂,所以我正在编写一种临时视图,并使用 OPENQUERY 语句来获取数据,所以最终我最终会循环这样的语句:
exec('INSERT INTO tabl SELECT * FROM OPENQUERY(@Server, @AdHocView)')
但是,我听说在本地服务器上使用 OPENQUERY 是不受欢迎的。有人可以详细说明为什么吗?
I'm writing a script that is supposed to run around a bunch of servers and select a bunch of data out of them, including the local server. The SQL needed to SELECT the data I need is pretty complicated, so I'm writing sort of an ad-hoc view, and using an OPENQUERY statement to get the data, so ultimately I end up looping over a statement like this:
exec('INSERT INTO tabl SELECT * FROM OPENQUERY(@Server, @AdHocView)')
However, I've heard that using OPENQUERY on the local server is frowned upon. Could someone elaborate as to why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sp_addlinkedserver
存储过程,则远程服务器上使用的凭据将被硬编码到脚本中,任何拥有副本的人都可以看到参考:
sp_addlinkedserver
stored procedure is used within same script, the credentials used on the remote server are hardcoded into the script, visible to anyone who has a copyReference:
除了@OMG Ponies 所说的之外,这根本没有必要。如果没有必要,没有理由引入即席查询和分布式事务语义。当您使用
OPENQUERY
时,您会承受动态SQL的所有负面影响,包括不太可预测的计划以及服务器无法准确跟踪依赖关系。OPENQUERY 还要求本地用户拥有目标服务器的权限,这可能不是您想要的,除非它是管理脚本。您不能期望一个数据库的每个用户都对其他每个数据库都具有相同的权限。
In addition to what @OMG Ponies said, it's simply unnecessary. There's no reason to introduce ad-hoc query and distributed transaction semantics when you don't have to. When you use
OPENQUERY
you take on all of the negative aspects of dynamic SQL, including less predictable plans and the server's inability to accurately track dependencies.OPENQUERY
also requires the local user to have permissions to the target server, which is probably not what you want unless it's an administrative script. You can't expect every user of one database to have the same permissions to every other database.只是后续。
当您必须比较或操作存储过程中的某些行集时,OpenQuery 非常有用。
例如,如果您在从 SQL Server 2005 迁移到 SQL Server 2008 时必须比较两台服务器(测试服务器和部署服务器)的结果,那么您可以执行以下操作:以下查询:
查看任何差异。
Just a followup.
OpenQuery is good when you have to compare or manipulate some rowsets from stored procedures.
for example if you have to compare results from two servers (test and rollout server) when you migrate from SQL Server 2005 to SQL server 2008 for example, then you can do the following query:
to see any discrepancies.