Insert Data From One Server To Another?

前提是你 提交于 2019-12-29 06:23:10

问题


If I want to run this sort of query in SQL Server, how can I do the same query from one server I am connected to to another?

I tried adding "[ServerName1]." before "[DatabaseName1].[dbo]..." and "[ServerName2]." before "[DatabaseName2].[dbo]..." but that didn't seem to work.

INSERT INTO [DatabaseName1].[dbo].[TableName]
           ([FieldName])
     SELECT [FieldName] FROM [DatabaseName2].[dbo].[TableName]

Is this possible?


回答1:


Yes you would use the server-name before the whole rest of object-name like:

myserver.mydatabase.dbo.mytable

However you first have to set up linked servers. Look up linked servers in BOL.




回答2:


If you have adhoc distributed queries enabled you can use OPENDATASOURCE. Setting up a linked server is another option. Not sure of the pros and cons of each approach.

INSERT INTO [DatabaseName1].[dbo].[TableName]
SELECT FieldName
FROM OPENDATASOURCE('SQLNCLI',
    'Data Source=Server\InstanceName;Integrated Security=SSPI')
    .DatabaseName2.dbo.TableName



回答3:


The best way to do this would be to create a "linked server" between the two. You will need appropriate permissions to do this.

Then it's just a matter of accessing the databases using your linkedserver name.

Ex: [linkedserver].databasename.dbo.tablename

To create a linkedserver, go to server objects->right click on linked servers->click on 'new linked server'.



来源:https://stackoverflow.com/questions/3285391/insert-data-from-one-server-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!