SQL - remote query

I would like to remote query a database with a SQL statement.  I would like to insert result from the query to my local SQL server.
My task.
1. connect to remote SQL server
2. perform a remote query
3. insert result  insert into a table of a local database.

How do I do that
tommym121Asked:
Who is Participating?
 
tigin44Connect With a Mentor Commented:
you can reference a remote server via linked servers..

first of all you should define a linked server to the remote server. you should find detatiled information in here
    http://msdn.microsoft.com/en-us/library/ms188279.aspx 
or at books online.

once you define linked server you should reference a database on that remote server by using four part name convention i.ei,
   [linkedServerName].[remoteDatabaseName].[schemaName].[databaseObjectName]


lets asume our linked server name is LS_SOME_SERVER... so aquery like this will do what you want

INSERT INTO dbo.localTableName (col1, col2, ...., coln)
SELECT col1, col2, ...., coln
FROM [LS_SOME_SERVER].[remoteDatabaseName].[dbo].[remoteTableName]
0
 
tommym121Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.