本文介绍了在sql server中生成下一个序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要生成一个序列号以用作主键(在遗留系统中)
I have a need to generate a sequence number to be used as a primary key (in a legacy system)
我想知道以下解决方案在竞争条件下是否会受到并发的影响.
I want to know if the following solution suffers from concurrency during race conditions.
CREATE TABLE SequenceGenerator
(
Sequence INT
)
INSERT INTO SequenceGenerator SELECT 0
这是我将在需要下一个序列号时调用的存储过程
And here is the stored procedure that i will be calling whenever i need the next sequence number
CREATE PROCEDURE GetNextSequence
AS
SET NOCOUNT ON
DECLARE @NextSequence INT
UPDATE SequenceGenerator SET
@NextSequence = Sequence,
Sequence = Sequence + 1
RETURN @NextSequence + 1
谢谢
推荐答案
为什么不使用 Identity 列?
IDENTITY [ (seed ,increment) ]
例如
CREATE TABLE SequenceGenerator
(
Sequence INT IDENTITY(1,1)
)
这篇关于在sql server中生成下一个序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!