唯一ID生成存储过程. [英] Unique ID generation stored procdeure .

查看:95
本文介绍了唯一ID生成存储过程.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1000之后,该值将再次重置为特定时间的计数器为1.每次提供唯一的ID时,此存储过程都将100%可靠吗?有多个访问Web应用程序的用户可以调用此存储过程.

After 1000 this will reset counter for specific term to 1 again. Will this stored procedure be 100% reliable to give unique id every time. There are multiple users accessing the web application that may call this stored procedure.

ALTER PROCEDURE [dbo].[get_next_id](@Term int,@ID INT OUTPUT) AS
SET NOCOUNT ON
DECLARE @Current int;
SELECT @Current=[ID] FROM [IdGenerator] where Term = @Term;
IF @Current<1000
BEGIN
    update IdGenerator SET @ID = ID, ID = ID + 1 where Term=@Term
END
ELSE
BEGIN
    update IdGenerator SET @ID =1,ID = 2 where Term=@Term
END




一种选择是使用身份"列,但是要生成唯一ID,这是毫无疑问的,我必须在每个数字中添加某些文本,因为这是业务需求的一部分.像在数字前加上1001或1002或1003等.此存储过程将为我提供唯一编号.
进行批评/建议其他选择.




One option is to use identity column but that is out of question as to generate unique Id I have to add certain text to each number as that is the part of business requirement. Like add 1001 or 1002 or 1003 etc. in front of numbers. This stored procedure will give me the unique number.
Criticize it / Suggest any alternative .

推荐答案

建议困难,因为您尚未说明Id的用途.

如果用于记录ID,则结果ID可能不是唯一的;其次,我认为我会在表上使用Insert触发器来生成UID,从而确保始终生成UID.
Difficult to advise, as you have not said what the Id is used for.

If it is for a record Id then there is a probability that the resulting Id will NOT be unique and secondly I think that I would have used a Insert trigger on the tables to generate the UID, thus ensuring the UID is always generated.


您正在做什么,但是我仍然会考虑在表上使用INSERT触发器,因为添加新学生时,这总是会触发,无论哪个程序/源插入了数据.
What you are doing is fine, but I would still consider using a INSERT trigger on the table, as this will always fire when a new student is added, no matter which program/source inserted the data.


这篇关于唯一ID生成存储过程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆