34

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.

Valentino Vranken
  • 5,597
  • 1
  • 26
  • 28
Boris Callens
  • 90,659
  • 85
  • 207
  • 305

8 Answers8

39

I think what you want to do is create a linked server as per this webarchive snapshot of msdn article from 2015 or this article from learn.microsoft.com. You would then select using a 4 part object name eg:

Select * From ServerName.DbName.SchemaName.TableName
Heavy JS
  • 51
  • 7
Matthew Pelser
  • 936
  • 6
  • 11
  • A bit late, but I only now revisited this question and yes, this looks like an answer to my question. Although by now I realise my question should have been the question to the answer that I selected. All getting a bit complicated... – Boris Callens Dec 15 '09 at 12:41
  • 4
    The link is broken :-( – Fabian Pijcke Apr 03 '19 at 06:41
  • 1
    yep, this is why people get flamed for link only answers – M H Apr 06 '21 at 17:15
  • 1
    Without the link working this answer basically has the same information the question already gives. – baltermia May 04 '21 at 13:21
  • https://learn.microsoft.com/en-us/sql/relational-databases/linked-servers/create-linked-servers-sql-server-database-engine?view=sql-server-ver15 In SQL Server Management Studio (SSMS): Open Object Explorer. Expand Server Objects. Right-click Linked Servers. Select New Linked Server. You can also create a Synonym in your Primary Database to point to your Linked Server. – Jack Feb 23 '22 at 18:40
8

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
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
  • 1
    Cool, didn't know this existed. On the other hand, http://technet.microsoft.com/en-us/library/ms179856.aspx says there have to be explicit regedits for that. – Boris Callens Sep 09 '13 at 11:20
1

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

Galwegian
  • 41,475
  • 16
  • 112
  • 158
1

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
kristof
  • 52,923
  • 24
  • 87
  • 110
1

Well I don't agree with your comment on replication. You can start a replication by creating a database from scratch, and you can control either the updates will be done by updating the available client database or simply recreating the database.

Automated replication will ease your work by automatically managing keys and relations.

I think the easiest thing to do is to start a snapshot replication through MSSQL Server Studio, get the T-SQL corresponding scripts (ie the corresponding T-SQL instructions for both publication and subscriptions), and record these scripts as part of a job in the Jobs list of the SQL Agent or as a replication job in the replications folder.

Philippe Grondier
  • 10,900
  • 3
  • 33
  • 72
1

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.

0

Can you use the Data Transformation Services to do the job? This provides all sorts of bolt-together tools for doing this kind of thing.

You can download the SQL Server 2005 feature pack from Microsoft's website here

Thomas Rushton
  • 5,746
  • 2
  • 25
  • 16
-1
CREATE VIEW newR1 
AS
SELECT * from OPENQUERY ([INSTANCE_NAME], 'select * from DbName.SchemaName.TableName')
ekad
  • 14,436
  • 26
  • 44
  • 46
  • This does require the Linked Server as well, so basically this is just another kind of query following the same suggestion already made some years ago. – Magier Jul 11 '17 at 08:04