SQL Server:将表内容从一个数据库复制到另一个数据库
我想用我们服务器的当前值更新本地开发数据库上的静态表(通过 VPN 在不同的网络/域上访问)。使用数据导入/导出向导将是我选择的方法,但是我通常会遇到两个问题之一:
- 出现主键违规错误,并且整个过程退出。这是因为它试图插入我已经拥有的行。
- 如果我在向导中设置“从目标删除”选项,则会收到外键冲突错误,因为其他表中存在引用这些值的行。
我想要的是一组正确的选项,这意味着导入/导出向导将更新存在的行并插入不存在的行(基于主键或询问我将哪些列用作键)。
我怎样才能做到这一点?这是在 SQL Server 2005 和 2008 上的(我确信它在 SQL Server 2000 DTS 向导上也可以正常工作)。
I want to update a static table on my local development database with current values from our server (accessed on a different network/domain via VPN). Using the Data Import/Export wizard would be my method of choice, however I typically run into one of two issues:
- I get primary key violation errors and the whole thing quits. This is because it's trying to insert rows that I already have.
- If I set the "delete from target" option in the wizard, I get foreign key violation errors because there are rows in other tables that are referencing the values.
What I want is the correct set of options that means the Import/Export wizard will update rows that exist and insert rows that do not (based on primary key or by asking me which columns to use as the key).
How can I make this work? This is on SQL Server 2005 and 2008 (I'm sure it used to work okay on the SQL Server 2000 DTS wizard, too).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定你可以在管理工作室中做到这一点。我有过一些很好的经历
RedGate SQL Data Compare 用于同步数据库,但您必须付费为了它。
I'm not sure you can do this in management studio. I have had some good experiences with
RedGate SQL Data Compare in synchronising databases, but you do have to pay for it.
SQL Server 数据库发布向导可以为您感兴趣的表导出一组sql插入脚本。只需告诉它仅导出数据而不导出模式即可。它还将创建必要的删除语句。
The SQL Server Database Publishing Wizard can export a set of sql insert scripts for the table that you are interested in. Just tell it to export just data and not schema. It'll also create the necessary drop statements.
一种选择是将数据下载到新表,然后使用类似于以下内容的命令来更新目标:
One option is to download the data to a new table, then use commands similar to the following to update the target:
如果您在第二个选项期间禁用 FK 约束 - 并在 finsih 之后恢复它们 - 它将起作用。
但是,如果您使用身份来创建 FK 中涉及的 pk,则会导致问题,因此仅当 pk 值保持不变时它才有效。
If you disable the FK constrains during the 2nd option - and resume them after finsih - it will work.
But if you are using identity to create pk that are involves in the FK - it will cause a problem, so it works only if the pk values remains the same.