Queries across linked servers in SQL Server 2005

27 March 2008

I wanted to do some quick queries with references to both a dev and staging server. To allow this I add the staging server as a linked server. Here's how.

In the development server, add a linked server with the following:

  EXEC sp_addlinkedserver 'THE_OTHER_SERVER', N'SQL Server'  

Note that you should get two "(1 row(s) affected)" messages.

Then navigate to:

  Server Objects > Linked Servers > Providers   

Here you should see THE_OTHER_SERVER listed. Right click on this and in the security tab add a user name and password. Click ok and then you should be able to access THE_OTHER_SERVER with the following syntax:

  SELECT * FROM THE_OTHER_SERVER.DATABASE.OWNER.TABLE  

Another note, I wanted to truncate a table using the above syntax but you will get a message:

  Msg 117, Level 15, State 1, Line 1  The object name 'THE_OTHER_SERVER.DATABASE.OWNER.' contains more than the maximum number of prefixes. The maximum is 2.  

I'm sure there are other limitations out there too.

And 2 minutes after posting I found another. Using SELECT INTO.