一种在SQLServer中实现Sequence的高效方法
如果在ORACLE里面用惯了Sequence的兄弟们,要在SqlServer里实现Sequence,就会发现没有现成的Sequence对象可以Create了。那应该怎么办呢?
当然这点小问题是难不倒我们程序员的,“max+1啊”,有人会说这样的方式。是的,这种方式实现起来也比较简单。当然你也许还会说,最Sql的方式是采用identity列,自增的方式去增加就ok了。但是这样的列如果要实现“YYYYMMDD”+Sequence值 (例如:2008072400001)的格式就不行了。还是老老实实用存储过程来取得一个Sequence值吧,使用起来也就随心所欲了。
网上常见的一个存储过程为 为当前库所有用到Sequence建一张表,譬如“AllSequence”,内含四个字段【名字,起点值,递增值,当前值】,创建Sequence的时候就创建一条记录,获取Sequence的时候就从对应行的当前值按递增值增加得到。
在并发性请求不那么高的系统里,这种过程是没有问题的。但是并发性请求一旦处于某个量级以后,这个过程常常会遇到问题。
以下为一个改进方法,适合于高吞吐量的访问请求,比如每秒数千次的请求:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
要获取刚才我们提到的 “20080724000056”这种格式的话,下面这样就可以了
select Convert(char(8),Getdate(),112) + right('00000'+CAST(@NewSeqVal AS varchar(5)),5) as mySeq
不过还是有用的不爽的一点地方,不能直接在非存储过程的select的语句中直接使用。
转自 http://www.cnblogs.com/heekui/archive/2008/07/24/1250842.html
还有一种思路
CREATE TABLE sequences (
-- sequence is a reserved word
seq varchar(100) primary key,
sequence_id int
);
MS SQL Server stored procedure:
CREATE PROCEDURE nextval
@sequence varchar(100),
@sequence_id INT OUTPUT
AS
-- return an error if sequence does not exist
-- so we will know if someone truncates the table
set @sequence_id = -1
UPDATE sequences
SET @sequence_id = sequence_id = sequence_id + 1
WHERE seq = @sequence
RETURN @sequence_id