SQL Server 定期重新启动自动编号
我需要一个正在开发的程序,将工作编号存储为 YY-######,每年从 000001 开始递增数字,前面加上年份的最后两位数字。
我能想到的唯一方法是创建一个 CurrentJob 表并使用标识列以及年份的最后两位数字和 ArchiveJob 表,然后通过视图中的并集将两者组合起来。 然后我必须在年初将 CurrentJob 复制到 ArchiveJob 并截断 CurrentJob。
是否有一种更简单的方法可以在一个表中重新启动编号(显然不是身份列)?
客户端在新年期间关闭,因此在年份更改时不应输入数据(对于学校)。
I have a requirement for a program I'm working on to store job numbers as YY-###### with incrementing numbers starting at 000001 in each year preceded by the last two digits of the year.
The only method I've been able to come up with is to make a CurrentJob table and use an identity column along with the last two digits of the year and an ArchiveJob table and then combining the two via a union in a view. Then I'd have to copy the CurrentJob to ArchiveJob at the begining of the year and truncate CurrentJob.
Is there an easier way to restart the numbering (obviously not having it be an Identity column) in one table?
The client is closed on New Years so there should be no data entry at the change of the year (for a school).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标识列是迄今为止在 SQL Server 内生成序列号最快、最并发的解决方案。 不过没有必要把事情搞得太复杂。 只需使用一张表来生成身份值,并在年底重置它。 这是一个简单的例子:
An identity column is by far the fastest and most concurrent solution to generating sequential numbers inside SQL Server. There's no need to make it too complicated though. Just have one table for generating the identity values, and reset it at the end of the year. Here's a quick example:
有一个类似的问题#761378。。 (注意:它使用MySql但原理是相同的)
接受的答案建议使用第二个表来管理当前ID。
但是最常见的问题是不要这样做!请注意 HLGEM 在帖子中的回答,了解为什么不这样做的原因。
There is a similar question #761378.. (Note: it uses MySql but the principle is the same)
The accepted answer suggested using a second table to manage the current ID.
However the most popular question was to not do this! Please note HLGEM's answer on the post for reasons why not to.
您可以使用此处中的“reseed”命令来重置起始种子价值。
You can use the "reseed" command from here to reset the starting value.