Using T-Sql, how can I insert from one table on a

2019-04-01 22:06发布

问题:

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.

回答1:

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'


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.