SQL 服务器完全限定名称中的列名

Column name in SQL Server fully qualified name

我在 SQL Server documentation 中读到我可以在简单 SELECT 查询的 FROM 子句中指定列名。 但是当我尝试 运行 这个查询时:

select * from my_db.dbo.test_table.test;

我收到以下错误:

Msg 7202, Level 11, State 2, Line 1 Could not find server 'my_db' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

我知道这是一个 T-SQL 页面,我正在尝试 运行 .

为什么会这样?

我正在使用 SQL 通过 Microsoft SQL Server Management Studio 2017 版的服务器版本。

请运行sp_addlinkedserversp_addlinkedsrvloginSP:

EXEC sp_addlinkedserver @server='MyLinkedServer'
EXEC sp_addlinkedsrvlogin 'MyLinkedServer', 'false', NULL, 'MyUserName', 'MyPassword'

之后可以尝试运行:select * from my_db.dbo.test_table

参考:https://blog.sqlauthority.com/2014/08/26/sql-server-fix-error-7202-could-not-find-server-in-sys-servers-verify-that-the-correct-server-name-was-specified/

此外,即使它是 SQL Server 2014 相关的也可能有帮助,请参阅:

您不能在 FROM 子句中指定列,但可以在 SELECT 子句中指定列。

select test from my_db.dbo.test_table

尝试从这里重写

select * from my_db.dbo.test_table.test;

至此

select test_table.test,* from my_db.dbo.test_table;

它是用那么多句点编写的,它假定您正在尝试完全限定 table 因此您尝试对服务器进行的处理如下

my db = linked server (a server other than the Server you are working on)
dbo = Schema (which is correct)
test_table = Table (Also correct)
test = just plain erroror

您要显示的字段应直接跟在关键字 Select 之后,因此如果您只想要 2 个字段,您可以写

select test,test2 from my_db.dbo.test_table;

如果您只有一台服务器,则更简单

select test,test2 from dbo.test_table;

或者如果您只有默认模式 dbo(数据库所有者)

select test,test2 from test_table;

希望泰尔普斯