简体   繁体   中英

Using T-Sql, how can I insert from one table on a remote server into another table on my local server?

Given the remote server 'Production' (currently accessible via an IP) and the local database 'Development', how can I run an INSERT into 'Development' from 'Production' using T-SQL?

I'm using MS SQL 2005 and the table structures are a lot different between the two databases hence the need for me to manually write some migration scripts.

UPDATE:

T-SQL really isn't my bag. I've tried the following (not knowing what I'm doing):

EXEC sp_addlinkedserver 
    @server = N'20.0.0.1\SQLEXPRESS', 
    @srvproduct=N'SQL Server' ;

GO

EXEC sp_addlinkedsrvlogin '20.0.0.1\SQLEXPRESS', 'false', 
    'Domain\Administrator', 'sa', 'saPassword'

SELECT * FROM [20.0.0.1\SQLEXPRESS].[DatabaseName].[dbo].[Table]

And I get the error:

Login failed for user ''. The user is not associated with a trusted SQL Server connection.

create a linked server and then use 4 part notation

insert table
select <column names>
from LinkedserverName.DatabaseName.SchemaName.TableName

you can also use OPENROWSET

example

insert table
SELECT a.*
FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
     'SELECT GroupName, Name, DepartmentID
      FROM AdventureWorks2008R2.HumanResources.Department
      ORDER BY GroupName, Name') AS a;

try this to create the login

EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'20.0.0.1\SQLEXPRESS',
@useself=N'False',
@locallogin=NULL,
@rmtuser=N'sa',
@rmtpassword='saPassword'

You can define the PROD Server as Linked Server to the DEV box and then access it. However I think it would be easier to get a backup from PROD Box and Restore it to DEV or use SSIS for Schema Import.

Look into the RedGate tools, esp. SQL Data Compare. If that's not an option you should look at OPENDATASOURCE or OPENROWSET to access the remote database.

Well you can use a linked server and then use the 4 part names for objects (See BOL for how to set up a linked server) Or you could use SSIS to set up the data migrations and connect to the remote server Or you could use OPENROWSET

I'd probably use SSIS, but I'm already familiar with it.

Use SSMS. Right click on the target DB and select "Tasks", "Import Data". You will be able to preview the data and make conversions visually. Save the package in SSIS or run it now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM