Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am trying dynamically build an SQL Statement that includes linked server. That works. When I use "localhost" to reference the current local machine SQL Server, that doesn't work.

For example:

Select * From [LOCALHOST\SQLEXPRESS].[My_Database].[dbo].[My_Table]

However, if I specify my machine name instead, it will work.

Select * From [My_Machine\SQLEXPRESS].[My_Database].[dbo].[My_Table]

Why can't I reference Localhost?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You can only reference servers that are listed under Server Objects -> Linked Servers as well as the local server via what you get back from @@SERVERNAME. Four part naming does not trigger a NETBIOS / DNS lookup. If you are referencing the local machine anyway, why not just use three part naming?

share|improve this answer
    
I have a store procedure that dynamically build up a sql statement by combine server name, database name, schema and table name together. Good thing the integration test failed due the server name parameter that we pass in is Localhost. I will have to figure out another way to build the sql. –  dsum Dec 6 '11 at 17:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.