开发者

How to reference a sql server with a backslash (\) in its name?

开发者 https://www.devze.com 2022-12-25 18:02 出处:网络
Givens: One SQL Server is named: DevServerA Another is named: DevServerB\\2K5 Problem: From DevServerA, how can I write a query that references DevServerB\\2K5?

Givens:

  • One SQL Server is named: DevServerA
  • Another is named: DevServerB\2K5

Problem:

From DevServerA, how can I write a query that references DevServerB\2K5?

I tried a sample, dummy query (running it from DevSe开发者_高级运维rverA):

SELECT TOP 1 *  
FROM DevServerB\2K5.master.sys.tables

And I get the error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '\.'.

However, I know my syntax is almost correct, since the other way around works (running this query from DevServerB\2K5):

SELECT TOP 1 *  
FROM DevServerA.master.sys.tables

Please help me figure out how to reference DevServerB\2K5 from DevServerA. Thanks.


In 4 part names, the first part if the name of a linked server (ie. a metadata object), not the name of a server (ie. a host name). So you can name your linked server FOO and have him point at the host BAR, or at the instance FOO\BAR. And even if you name the linked server object to contain a slash, you can still use it in a multi-part name by simply quoting the name:

SELECT TOP 1 *  
FROM [DevServerB\2K5].master.sys.tables


Try using square brackets:

SELECT TOP 1 *  
FROM [DevServerB\2K5].master.sys.tables


On SQL SERVER 2005, the following happened: Entry of:

SELECT TOP 1 *   
FROM [DevServerB\2K5].master.sys.tables 

Is changed to

SELECT TOP 1 *   
FROM DevServerB\2K5.master.sys.tables 

by SQL Server system and you still get the error message: Incorrect syntax near '.'.

I tried it with a linked server named in two different ways: '[DevServerB\2K5]' and 'DevServerB\2K5'

Does anybody have any other ideas?

Thank you Alan Robertson

CORRECTION added the next day: I was wrong, partially. When one tries to create a view using a SQL statement like:

SELECT  *
FROM [DevServerB\2K5].TestDB.dbo.tables 

then the [ and ] are removed and one cannot save the view, BUT if one just writes a query using the same SQL string then it works correctly.

I was also able to execute a SQL statement like:

INSERT INTO [DevServerB\2K5].TestDB.dbo.tables ( ... ) ...

I can do what I wanted, but it would have been much better if I could have saved a view and used a view that would then be used for SELECT, INSERT and UPDATE of the table in the [DevServerB\2K5].TestDB database from the original server where I tried and failed to create a view because of the '\' .

-ASR-

0

精彩评论

暂无评论...
验证码 换一张
取 消