将数据从一个数据库表复制到另一数据库表?

发布于 2024-10-01 09:23:44 字数 113 浏览 1 评论 0原文

我想从 DB ANIMAL1 复制表 DOG 并将数据复制/创建到 DB ANIMAL2 中的表 DOG。

该表还需要复制所有行(主键)ID。

我该怎么办?

谢谢。

I want to copy Table DOG from DB ANIMAL1 and copy/create the data to Table DOG in DB ANIMAL2.

The Table needs to duplicate all rows (Primary Key) ID as well.

How do I go about that?

Thanks.

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

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

发布评论

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

评论(3

宁愿没拥抱 2024-10-08 09:23:44

如果两个服务器位于同一网络上,您可以创建一个从目标服务器到源服务器的“链接服务器”,然后您可以编写如下内容:

INSERT INTO dbo.DOG(list of columns)
   SELECT (list of columns)
   FROM SourceServer.ANIMAL2.dbo.DOG

如果您的 ID 是 IDENTITY,您必须在命令之前打开 IDENTITY_INSERT:

SET IDENTITY_INSERT dbo.DOG ON

INSERT INTO dbo.DOG(list of columns)
   SELECT (list of columns)
   FROM SourceServer.ANIMAL2.dbo.DOG

SET IDENTITY_INSERT dbo.DOG OFF

并在 INSERT 命令运行后将其重新关闭。

If the two servers are on the same network, you could created a "linked server" from e.g. your target server to your source server, and then you could write something like:

INSERT INTO dbo.DOG(list of columns)
   SELECT (list of columns)
   FROM SourceServer.ANIMAL2.dbo.DOG

If your ID is an IDENTITY, you have to turn on IDENTITY_INSERT before the command:

SET IDENTITY_INSERT dbo.DOG ON

INSERT INTO dbo.DOG(list of columns)
   SELECT (list of columns)
   FROM SourceServer.ANIMAL2.dbo.DOG

SET IDENTITY_INSERT dbo.DOG OFF

and turn it back off after the INSERT command ran.

尛丟丟 2024-10-08 09:23:44

我认为 bcp 会很好地工作。显然,如果表已经包含目标上的行并且您要从源中追加更多行,那么如果您违反主键约束,它会抱怨

您没有说是否要执行一次或多次,如果一次,那么BCP 可能是您最好的选择,如果一直以来,链接服务器甚至复制可能是您最好的选择

bcp would work fine I think. clearly if the table already contains rows on the target and you are appending more rows from the source, it will complain if you violate the primary key contraint

you don't say if you want to do this once, or lots, if once, then BCP would probably be your best, if all the time, the linked servers, or even replication is probably your best bet

何处潇湘 2024-10-08 09:23:44

除了 marc_s 的回答之外,您还可以通过导出和导入数据SSMS 的向导。

in addition to marc_s' answer, you can also export and import data through SSMS' wizard.

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