Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is using OPENQUERY on a local server bad?

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?

like image 439
Dlongnecker Avatar asked Mar 03 '10 20:03

Dlongnecker


People also ask

Why you shouldn't use linked servers?

Problem #2: linked servers don't cache data. Even worse, it penalizes both servers involved with the linked server query. It's hard on the local server, and it's hard on the remote server that holds the single source of truth for the table.

What is the use of Openquery in SQL Server?

Executes the specified pass-through query on the specified linked server. This server is an OLE DB data source. OPENQUERY can be referenced in the FROM clause of a query as if it were a table name. OPENQUERY can also be referenced as the target table of an INSERT, UPDATE, or DELETE statement.

Are Linked servers a security risk?

Depending on the permissions that such a remote login has at the linked server, this could potentially pose a serious security threat. It is especially dangerous if your linked servers use a login that has the sysadmin role at the remote server.

Why do we use Openquery?

The OPENQUERY function is used to specify that a distributed query be processed on the remote server instead of the local server. Linked servers (four part queries); are also called distributed queries. Distributed queries create an execution plan by looking at the query and breaking it into remote and local queries.


1 Answers

  • 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:

  • OPENQUERY
like image 192
OMG Ponies Avatar answered Oct 16 '22 07:10

OMG Ponies