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. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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


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.