从 SQL Server Express 复制数据保留主键
我想将数据从一个数据库复制到另一个数据库,但我想保留原始数据库中的主键,这样就不会扰乱关系。我正在使用 SQL Server 2008。
I want to copy data from a database to another, but I'd like to keep the primary key from the original database so it won't mess with the relationships. I'm using SQL Server 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如已经提到的,IDENTITY_INSERT 是您要为每个表设置的标志,以允许您在每个插入记录的标识列上设置特定值。
但是,如果您只是尝试复制整个现有数据库(或大部分),而不仅仅是复制几个表,我建议只对数据库进行完整备份,将 BAK 文件恢复到新的数据库名称,然后对克隆进行修改。
As already mentioned, IDENTITY_INSERT is the flag you'll want to set for each table to allow you to set a particular value on the identity column for each inserted record.
But if you're merely trying to duplicate an entire existing database (or most of it), not just copy a few tables, I recommend just doing a full backup of the database, restore the BAK file to a new database name, and then make your modifications to the clone.
我假设你的主键是 IDENTITY?如果是这样,您应该使用 SET IDENTITY_INSERT ON对于表(一次仅一个表) - 如果您使用 SSIS 或类似工具进行传输,通常会有一个选项允许您插入身份。
I assume your primary keys are IDENTITY? If so, you should use SET IDENTITY_INSERT ON for the table (only one table at a time) - if you use a tool like SSIS or similar for the transfer, there is usually an option to allow you to insert identities.
您可以使用 bcp 命令来完成这项工作。使用 bcp 命令,您可以批量复制数据。该命令有一个保留您身份的开关。
要查看更多详细信息,请访问此链接。
you can use
bcp
command for this work. with bcp command you can bulk copy your data. this command have a switch that keep your identity.for see more details go to this link.