码迷,mamicode.com
首页 > 数据库 > 详细

Sqlserver模拟Sequence

时间:2015-03-05 20:49:04      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

create table Sequence_OA_Merchants_EnterpriseBasic(

      -- ID列为自增列
      SeqID int identity(1,1) primary key,
      -- Sequence值
      SeqVal varchar(1)
)
CREATE FUNCTION fn_GetNextSequenceVal_OA_Merchants_EnterpriseBasic
as
begin
      -- 声明新Sequence值变量
      declare @NewSeqValue int

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

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

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

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

-- 返回新Sequence值
return @NewSeqValue
end
declare @abc varchar(10);
exec @abc = proc_GetNextSequenceVal_OA_Merchants_EnterpriseBasic;
select right(replicate(0,5)+@abc,5);
select * from Sequence_OA_Merchants_EnterpriseBasic
insert into Sequence_OA_Merchants_EnterpriseBasic(SeqVal) values(a)
select * from Sequence_OA_Merchants_EnterpriseBasic

dbcc checkident(Sequence_OA_Merchants_EnterpriseBasic,reseed,-1)  重置Identity 

 

Sqlserver模拟Sequence

标签:

原文地址:http://www.cnblogs.com/JJ.Net/p/4316547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!