将数据从一个数据库表复制到另一数据库表?
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果两个服务器位于同一网络上,您可以创建一个从目标服务器到源服务器的“链接服务器”,然后您可以编写如下内容:
如果您的 ID 是
IDENTITY
,您必须在命令之前打开 IDENTITY_INSERT:并在 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:
If your ID is an
IDENTITY
, you have to turn on IDENTITY_INSERT before the command:and turn it back off after the INSERT command ran.
我认为 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
除了 marc_s 的回答之外,您还可以通过导出和导入数据SSMS 的向导。
in addition to marc_s' answer, you can also export and import data through SSMS' wizard.