Cross-server SQL

2020-05-19 21:45发布

I want to port data from one server's database to another server's database. The databases are both on a different mssql 2005 server. Replication is probably not an option since the destination database is generated from scratch on a [time interval] basis.

Preferebly I would do something like

insert *
from db1/table1
into db2/table2
where rule1 = true

It's obvious that connection credentials would go in somehwere in this script.

8条回答
\"骚年 ilove
2楼-- · 2020-05-19 22:06

You can use Open Data Source Like this :

EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO

EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO


SELECT  *
FROM    OPENDATASOURCE('SQLOLEDB',
                   'Data Source=<Ip Of Your Server>;
                    User ID=<SQL User Name>;Password=<SQL password>').<DataBase name>.<SchemaName>.<Table Or View Name>

Go
查看更多
疯言疯语
3楼-- · 2020-05-19 22:10

I think what you want to do is create a linked server as per this msdn article. You would then select using a 4 part object name eg:

Select * From ServerName.DbName.SchemaName.TableName
查看更多
老娘就宠你
4楼-- · 2020-05-19 22:11

You could go the linked server route.

you just can't use the select * into you have to do an insert into select.

I would avoid replication if you don't have experience with it as it can be difficult to fix if it breaks and can be prone to other problems if not properly managed.

Keep it simple especially if the databases are small.

查看更多
仙女界的扛把子
5楼-- · 2020-05-19 22:11
CREATE VIEW newR1 
AS
SELECT * from OPENQUERY ([INSTANCE_NAME], 'select * from DbName.SchemaName.TableName')
查看更多
祖国的老花朵
6楼-- · 2020-05-19 22:17

Are SQL Server Integration Services (SSIS) an option? If so, I'd use that.

查看更多
成全新的幸福
7楼-- · 2020-05-19 22:20

Would you be transferring the whole content of the database from one server to another or just some data from a couple of tables?

For both options SSIS would do the job especially if you are planning to to the transfer on a regular basis.

If you simply want to copy some data from 1 or 2 tables and prefer to do it using TSQL in SQL Management Studio then you can use linked server as suggested by pelser

  1. Set up the source database server as a linked server
  2. Use the following syntax to access data
select columnName1, columnName2, etc from serverName.databaseName.schemaName.tableName
查看更多
登录 后发表回答