为什么在本地服务器上使用 OPENQUERY 不好?

发布于 2024-08-23 21:18:49 字数 273 浏览 4 评论 0原文

我正在编写一个脚本,该脚本应该在一堆服务器上运行并从中选择一堆数据,包括本地服务器。选择我需要的数据所需的 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 技术交流群。

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

发布评论

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

评论(3

小兔几 2024-08-30 21:18:49
  • 尽管查询可能返回多个结果集,但 OPENQUERY 仅返回第一个结果集。
  • OPENQUERY 不接受变量作为其参数。
  • OPENQUERY 不能用于在链接服务器上执行扩展存储过程。但是,可以使用四部分名称在链接服务器上执行扩展存储过程。
  • 如果在同一脚本中使用 sp_addlinkedserver 存储过程,则远程服务器上使用的凭据将被硬编码到脚本中,任何拥有副本的人都可以看到

参考:

  • Although the query may return multiple result sets, OPENQUERY returns only the first one.
  • OPENQUERY does not accept variables for its arguments.
  • OPENQUERY cannot be used to execute extended stored procedures on a linked server. However, an extended stored procedure can be executed on a linked server by using a four-part name.
  • If the 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 copy

Reference:

随风而去 2024-08-30 21:18:49

除了@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.

爱*していゐ 2024-08-30 21:18:49

只是后续。

当您必须比较或操作存储过程中的某些行集时,OpenQuery 非常有用。

例如,如果您在从 SQL Server 2005 迁移到 SQL Server 2008 时必须比较两台服务器(测试服务器和部署服务器)的结果,那么您可以执行以下操作:以下查询:

select * into test_table from OpenQuery(testServer, 'exec testdb.dbo.test_sp');
select * into rollout_table from OpenQuery(rolloutServer, 'exec testdb.dbo.test_sp');

select * from test_table
except
select * from rollout_table;

select * from rollout_table
except
select * from test_table;

查看任何差异。

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:

select * into test_table from OpenQuery(testServer, 'exec testdb.dbo.test_sp');
select * into rollout_table from OpenQuery(rolloutServer, 'exec testdb.dbo.test_sp');

select * from test_table
except
select * from rollout_table;

select * from rollout_table
except
select * from test_table;

to see any discrepancies.

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