批量修改SQL Server中的唯一ID
[这是一个有点不寻常的问题,我知道...]
我需要一个脚本,它将把我们数据库中的每个唯一的 id 值更改为新的值。 问题是我们的配置表可以在 id 敏感的软件实例之间导出(破坏现有的 id)。 几年前,我们在开发“标准配置”和客户实例之间设置了“足够宽”的 id 差距,但现在已经不够宽了:( - 例如,当我们遇到 id 冲突时客户端导入我们的标准配置
绝对是我们可以做的最简单/最短时间的事情,例如修复代码太复杂并且容易出错,请注意,我们并没有“消除”。这里的问题只是将差距从 1000 更改为 1000000 或更多(现有的差距需要 5 年才能填补
)
- 。脚本)
- 创建一个具有我们想要的新的最低 id 的标识表
- 对于每个表,将 id 修改为标识表中的下一个标识(必要时使用标识插入修饰符标志)也许在处理每个表后,我们可以重置标识表。 。
- 关闭UPDATE_CASCADE,并删除身份表
我正在为此寻找任何(部分或完整)脚本。
[This is a bit of an unusual problem, I know...]
What I need is a script that will change every unique id value to new one in our database. The problem is that we have configuration tables that can be exported between instances of our software which is id-sensitive (clobbering existing ids). Years ago, we set up a "wide-enough" id gap between our development "standard configuration" and our client's instances, which is now not wide enough :( - e.g. we're getting id conflicts when clients import our standard configuration.
A SQL script to do the following is definitely the simplest/shortest-timeframe thing that we can do. e.g. fixing the code is far too complicated and error prone to consider. Note that we are not "eliminating" the problem here. Just changing the gap from 1000's to 1000000's or more (the existing gap took 5 years to fill).
I believe the simplest solution would be to:
- change all our tables to UPDATE_CASCADE (none of them are - this will greatly simplify the script)
- create an identity table with the new lowest id that we want
- For each table, modify the id to the next one in the identity table (using identity insert modifier flags where necessary). Perhaps after each table is processed, we could reset the identity table.
- turn off UPDATE_CASCADE, and delete the identity table.
I am seeking any (partial or full) scripts for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,UPDATE_CASCADE 在 Sql Server 的世界中不存在。 我建议您为每个要重新键入的表执行以下操作(伪代码)
检查一切是否仍然有效...
此过程可以通过 DMO/SMO 对象自动执行,但根据涉及的表数量,我会说使用Management Studio 生成可以编辑的脚本可能会更快。 毕竟,您只需每 5 年执行一次即可。
Unfortunately UPDATE_CASCADE doesn't exist in the world of Sql Server. I suggest for each table you to re-key you do the following (Pseudo Code)
Check it all still works ...
This process could be automated through DMO/SMO objects, but depending on the number of tables involved I'd say using management studio to generate scripts that can then be edited is probably quicker. After all, you only need to do this once/5 years.
这里我们使用 SQL 2005 的代码。它很大,很hacky,但是它会工作(除非你有一个由其他两个主键组合而成的主键)。
如果有人可以用 MrTelly 更快的 id 添加(这不需要从游标为每个更新的行构建 sql)来重写它,那么我会将其标记为已接受的答案。 (如果我没有注意到新答案,请投票 - 然后我会注意到:))
Here we go with the code for SQL 2005. It's huge, it's hacky, but it will work (except in the case where you have a primary key that is a composite of two other primary keys).
If someone can re-write this with MrTelly's faster id addition (which wouldn't require building sql from a cursor for each updated row), then I'll mark that as the accepted answer. (If I don't notice the new answer, upvote this - then I'll notice :))
为什么不使用负数作为标准配置值并继续使用正数作为其他值?
Why don't you use negative numbers for your standard configuration values and continue to use positive numbers for other things?