[英]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.
如果这不是一个选项,您应该查看OPENDATASOURCE或OPENROWSET来访问远程数据库。
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.