连接到另一台服务器的 TSQL 命令 (SQL Server 2005)

2021-09-10 00:00:00 sql tsql sql-server

是否有连接到另一台服务器的 TSQL 命令?

或者当您在查询窗口中时,连接到另一台服务器并显示查询窗口的键盘快捷键是什么?

我已经看到 Ctrl+N 在某些屏幕中弹出连接到服务器对话框,但是当我已经在查询窗口中并按 Ctrl+N 时,它只会打开另一个查询窗口.>

USE 命令可以让您连接到当前服务器上的其他数据库,但是否有一个命令可以让您连接到另一台服务器?

我使用的是 SQL Server 2005.

解决方案

您可以使用 OpenDataSource 带有链接服务器

OpenDataSource(provider_name, init_string)

例如

SELECT名,性别从开放数据源 ('SQLOLEDB','DataSource = NOLI\SQL2;UserID=myUserID;Password=myPassword').Organisation.dbo.Employees

来自 MSDN-

<块引用>

和 OPENROWSET 函数一样,OPENDATASOURCE 应该只引用访问的 OLE DB 数据源很少.定义链接服务器对于访问更多的任何数据源比几次.两者都不OPENDATASOURCE 或 OPENROWSET 提供链接服务器的所有功能定义,例如安全性管理和查询能力目录信息.所有连接信息,包括密码,必须每次提供OPENDATASOURCE 被调用.

Is there a TSQL command to connect to another server?

Or when you are in a Query Window, what are the Keyboard shortcuts to connect to another server and have a Query Window show up?

I have seen Ctrl+N pop up the Connect to Server dialog in some screens but when I am in a Query Window already and hit Ctrl+N it just opens up another Query Window.

The USE command lets you connect to other databases on the current server but is there a command that lets you connect to another server?

I am using SQL Server 2005.

解决方案

You can use OpenDataSource with a linked server

OpenDataSource(provider_name, init_string)

For example

SELECT
FirstName,
Gender
FROM
OpenDataSource (
'SQLOLEDB',
'DataSource = NOLI\SQL2;UserID=myUserID;Password=myPassword'
).Organisation.dbo.Employees

From MSDN-

Like the OPENROWSET function, OPENDATASOURCE should only reference OLE DB data sources that are accessed infrequently. Define a linked server for any data sources accessed more than several times. Neither OPENDATASOURCE nor OPENROWSET provide all the functionality of linked-server definitions, such as security management and the ability to query catalog information. All connection information, including passwords, must be provided every time that OPENDATASOURCE is called.

相关文章