使用 T-Sql,如何从远程服务器上的一个表插入到本地服务器上的另一个表中? [英] Using T-Sql, how can I insert from one table on a remote server into another table on my local server?

查看:24
本文介绍了使用 T-Sql,如何从远程服务器上的一个表插入到本地服务器上的另一个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于远程服务器Production"(当前可通过 IP 访问)和本地数据库Development",我如何使用 T-SQL 从Production"运行 INSERT 到Development"?

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?

我使用的是 MS SQL 2005,两个数据库之间的表结构有很大不同,因此我需要手动编写一些迁移脚本.

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.

更新:

T-SQL 真的不是我的包.我已经尝试了以下(不知道我在做什么):

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]

我得到了错误:

用户 '' 登录失败.用户是不与受信任的 SQL 关联服务器连接.

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

推荐答案

创建链接服务器然后使用4部分符号

create a linked server and then use 4 part notation

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

您也可以使用 OPENROWSET

示例

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;

试试这个来创建登录

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

这篇关于使用 T-Sql,如何从远程服务器上的一个表插入到本地服务器上的另一个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆