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

发布于 2024-10-08 23:23:55 字数 610 浏览 0 评论 0原文

给定远程服务器“生产”(当前可通过 IP 访问)和本地数据库“开发”,如何使用 T-SQL 从“生产”运行 INSERT 到“开发”?

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

更新:

T-SQL 确实不是我的菜。我尝试了以下操作(不知道我在做什么):

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 关联 服务器连接。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ぺ禁宫浮华殁 2024-10-15 23:23:55

创建一个 链接服务器,然后使用 4 部分表示法,

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

您也可以使用 < href="http://msdn.microsoft.com/en-us/library/ms190312.aspx" rel="nofollow">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'

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'
岁月苍老的讽刺 2024-10-15 23:23:55

您可以将 PROD 服务器定义为 DEV 框的链接服务器,然后访问它。
不过,我认为从 PROD Box 获取备份并将其恢复到 DEV 或使用 SSIS 进行架构导入会更容易。

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.

放赐 2024-10-15 23:23:55

查看 RedGate 工具,尤其是。 SQL 数据比较。如果这不是一个选项,您应该查看 OPENDATASOURCEOPENROWSET 访问远程数据库。

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.

污味仙女 2024-10-15 23:23:55

那么您可以使用链接服务器,然后使用对象的 4 个部分名称(有关如何设置链接服务器的信息,请参阅 BOL)
或者您可以使用 SSIS 设置数据迁移并连接到远程服务器
或者您可以使用 OPENROWSET

我可能会使用 SSIS,但我已经熟悉它了。

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.

雨后咖啡店 2024-10-15 23:23:55

使用SSMS。右键单击目标数据库并选择“任务”、“导入数据”。您将能够预览数据并直观地进行转换。将包保存在 SSIS 中或立即运行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文