简体   繁体   English

如何在另一个数据库服务器中运行T-SQL?

[英]How to run T-SQL in another DB server?

I want to select some data form another database in another server. 我想从另一个服务器中的另一个数据库中选择一些数据。

What I know is add a Linked Server, and run the T-SQL. 我所知道的是添加一个链接服务器,然后运行T-SQL。

Is this way is a good choice, or is there any other way to run SQL in other database servers? 这是一个不错的选择,还是有其他方法可以在其他数据库服务器中运行SQL?

thanks. 谢谢。

Adding a linked server is fine. 添加链接服务器很好。

You will then probably use the 4 part naming convention 然后,您可能会使用4部分命名约定

select * from [server].[database].[Owner or Schema].[tablename]

Or you could look at OPENROWSET (Transact-SQL) 或者您可以查看OPENROWSET(Transact-SQL)

Includes all connection information that is required to access remote data from an OLE DB data source. 包括从OLE DB数据源访问远程数据所需的所有连接信息。 This method is an alternative to accessing tables in a linked server and is a one-time, ad hoc method of connecting and accessing remote data by using OLE DB. 此方法是访问链接服务器中的表的替代方法,并且是使用OLE DB连接和访问远程数据的一次性临时方法。 For more frequent references to OLE DB data sources, use linked servers instead. 要更频繁地引用OLE DB数据源,请改用链接服务器。

How 怎么样

The upsides 优点

  • Can distribute load across multiple servers 可以在多个服务器之间分配负载

The downsides 缺点

  • Additional network / communication / security overhead 额外的网络/通信/安全开销
  • Additional operational overhead - users, logins etc need to be added to both servers 额外的运营开销-用户,登录名等都需要添加到两台服务器中
  • Fragility - you are coupling code in your local database to a named external server / database. 易碎性-您正在将本地数据库中的代码耦合到命名的外部服务器/数据库。 However, you can use synonyms to loosen the coupling. 但是,您可以使用同义词来放松耦合。

Alternatives 备择方案

  • Use Replication / Mirroring / Log shipping to get a 'local' copy of the database on the same server (although this also has the downside of additional IO and memory consumption on the local server or instance, plus additional issues dealing with data synchronisation) 使用复制/镜像/日志传送在同一服务器上获取数据库的“本地”副本(尽管这样做还存在本地服务器或实例上额外的IO和内存消耗以及处理数据同步的其他问题)
  • Use 2 connection strings in your app and do the data manipulation in the app. 在您的应用中使用2个连接字符串,然后在应用中进行数据处理。

But as astander says, there is no real technical issue in accomplishing cross server sql. 但是正如旁观者所言,完成跨服务器sql并没有真正的技术问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM