惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Vulnerabilities – Threatpost
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy International News Feed
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Secure Thoughts
MyScale Blog
MyScale Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LINUX DO - 最新话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Cloudflare Blog
美团技术团队
Recorded Future
Recorded Future
T
Tailwind CSS Blog
Latest news
Latest news
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
Cloudbric
Cloudbric
Schneier on Security
Schneier on Security
I
Intezer
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
云风的 BLOG
云风的 BLOG
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
L
LangChain Blog
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
SecWiki News
SecWiki News
V2EX - 技术
V2EX - 技术
IT之家
IT之家
Cyberwarzone
Cyberwarzone
F
Full Disclosure
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志

博客园 - 网际浪人

.Net Framework 4.0 中利用Task实现并行处理、串并行混合处理 C# Process调用应用程序失败时应注意的问题 程序员的幽默 To腾讯:强行收集用户个人隐私的行为不可饶恕 VS2005打开VS2008项目的2种方法(转) 【转】Oracle Conversion Functions 晨星、银河基金业绩排行榜数据转换工具 ORACLE纯SQL实现多行合并一行 ASP.NET项目添加Log4Net后,发布后无法写日志 “必应”不应、“谷歌”不歌 你好2009,再见2008,牛年犇犇犇 oledb使用Access更新和插入操作的注意点 5.12大地震——小学二年级小表妹谢可欣的诗 GridView自动排序 [转]SQL Server 2005链接字符串 对HtmlEncode的增强——HtmlEntitiesEncode GridView中使用DataKeyNames存储数据键值 C#调用Excel VBA宏 封装SoapException处理Webservice异常
一种在SQLServer中实现Sequence的高效方法
网际浪人 · 2008-07-24 · via 博客园 - 网际浪人

如果在ORACLE里面用惯了Sequence的兄弟们,要在SqlServer里实现Sequence,就会发现没有现成的Sequence对象可以Create了。那应该怎么办呢?

当然这点小问题是难不倒我们程序员的,“max+1啊”,有人会说这样的方式。是的,这种方式实现起来也比较简单。当然你也许还会说,最Sql的方式是采用identity列,自增的方式去增加就ok了。但是这样的列如果要实现“YYYYMMDD”+Sequence值 (例如:2008072400001)的格式就不行了。还是老老实实用存储过程来取得一个Sequence值吧,使用起来也就随心所欲了。

网上常见的一个存储过程为 为当前库所有用到Sequence建一张表,譬如“AllSequence”,内含四个字段【名字,起点值,递增值,当前值】,创建Sequence的时候就创建一条记录,获取Sequence的时候就从对应行的当前值按递增值增加得到。

在并发性请求不那么高的系统里,这种过程是没有问题的。但是并发性请求一旦处于某个量级以后,这个过程常常会遇到问题。

以下为一个改进方法,适合于高吞吐量的访问请求,比如每秒数千次的请求:

-- 假定要给T_0101001创建一个Sequence

-- 创建表SeqT_0101001
create table SeqT_0101001(

      
-- ID列为自增列
      SeqID int identity(1,1primary key,

      
-- Sequence值
      SeqVal varchar(1)
)

-- 创建从SeqT_0101001表获取最新Sequence值的存储过程
create procedure P_GetNewSeqVal_SeqT_0101001
as
begin
      
-- 声明新Sequence值变量
      declare @NewSeqValue int

      
-- 设置插入、删除操作后的条数显示取消
      set NOCOUNT ON

      
-- 插入新值到SeqT_0101001表
      insert into SeqT_0101001 (SeqVal) values ('a')   

      
-- 设置新Sequence值为插入到SeqT_0101001表的标识列内的最后一个标识值  
      set @NewSeqValue = scope_identity()   

      
-- 删除SeqT_0101001表(不显示被锁行)
      delete from SeqT_0101001 WITH (READPAST)

-- 返回新Sequence值
return @NewSeqValue

end

--使用Sequence
Declare @NewSeqVal int
Exec @NewSeqVal =  P_GetNewSeqVal_SeqT_0101001
Print @NewSeqVal

要获取刚才我们提到的 “20080724000056”这种格式的话,下面这样就可以了

select Convert(char(8),Getdate(),112) + right('00000'+CAST(@NewSeqVal AS varchar(5)),5) as mySeq

不过还是有用的不爽的一点地方,不能直接在非存储过程的select的语句中直接使用。