简体   繁体   English

使用T-Sql,如何从远程服务器上的一个表插入本地服务器上的另一个表?

[英]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? 鉴于远程服务器'Production'(目前可通过IP访问)和本地数据库'Development',如何使用T-SQL从'Production'运行INSERT到'Development'?

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. 我正在使用MS SQL 2005,两个数据库之间的表结构有很大不同,因此需要我手动编写一些迁移脚本。

UPDATE: 更新:

T-SQL really isn't my bag. T-SQL真的不是我的包。 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. 用户未与受信任的SQL Server连接关联。

create a linked server and then use 4 part notation 创建链接服务器 ,然后使用4部分表示法

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

you can also use OPENROWSET 你也可以使用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. 您可以将PROD服务器定义为DEV框的链接服务器,然后访问它。 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. 但是我认为从PROD Box获取备份并将其还原到DEV或使用SSIS进行Schema Import会更容易。

Look into the RedGate tools, esp. 查看RedGate工具,尤其是 SQL Data Compare. SQL数据比较。 If that's not an option you should look at OPENDATASOURCE or OPENROWSET to access the remote database. 如果这不是一个选项,您应该查看OPENDATASOURCEOPENROWSET来访问远程数据库。

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 那么你可以使用链接服务器,然后使用对象的4部分名称(请参阅BOL了解如何设置链接服务器)或者您可以使用SSIS设置数据迁移并连接到远程服务器或者您可以使用OPENROWSET

I'd probably use SSIS, but I'm already familiar with it. 我可能会使用SSIS,但我已经熟悉它了。

Use SSMS. 使用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. 将包保存在SSIS中或立即运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务器复制表并插入另一台服务器:此T-SQL查询出了什么问题? - Copy Table from a Server and Insert into another Server: What is wrong with this T-SQL query? 如何在 SQL 服务器中使用自动增量从一个表插入到另一个表 - How can I insert into from one table to another with autoincrement in SQL Server T-SQL查询以从一个表中选择并插入到另一个表中 - T-SQL query to select from one table and insert into another SQL Server从一个表复制到另一个表,而无需使用插入 - SQL Server Copy from one table to another without using insert into 如何使用插入查询将数据从一个 SQL 表插入另一个表 - How can I insert data from one SQL table to another table using Insert query 如何将 SQL Server 存储过程的结果集从一台服务器导出到另一台服务器上的表中? - How can i export the result set of an SQL Server stored procedure from one server into a table on another server? SQL Server:从一个表到另一个表的INSERT - SQL Server : INSERT from one table to another table T-SQL:我可以使用另一个表中的行来更新表吗? - T-SQL: Can I update a table using rows from another table? 使用join update或插入SQL Server从另一个表插入一个表 - insert into one table from another table using join update or insert SQL server 如何在SQL Server 2012中将数据从另一个表的多个表行插入一个表行? - How to insert data into one table row from multiple table rows in another table in SQL Server 2012?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM