视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
MSSqlServer伪序列_MySQL
2020-11-09 20:12:33 责编:小采
文档


MSSqlServer伪序列_MySQL

先创建一个序列表
if exists (select 1
from sysindexeswhere 
id = object_id('TSysSequence')and 
name = 'In_SeName'and indid > 0and indid < 255)drop 
index TSysSequence.In_SeNamegoif exists 
(select 1from sysobjectswhere id = object_id('TSysSequence')and type = 'U')drop table TSysSequencego
/*======================================*//* Table: TSysSequence *//*=======================================*/
create table TSysSequence (SeName nvarchar(50) not null,
Increment int not null default 1,CurVal bigint not null default 0)goif exists 
(select 1 from sys.extended_propertieswhere major_id = object_id('TSysSequence') 
and minor_id = 0)begindeclare @CurrentUser sysnameselect @CurrentUser = user_name()execute 
sp_dropextendedproperty 'MS_Description','user', @CurrentUser, 'table', 'TSysSequence'endselect 
@CurrentUser = user_name()execute sp_addextendedproperty 'MS_Description','

模拟oracle序列

不允许用户维护,数据库初始化以后不允许任何人修改其中的值。

默认生成名称为“DID”和“SID”的两个序列,意义为“数据序列号”和“系统序列号”。',

'user', @CurrentUser, 'table', 'TSysSequence'goinsert into TSysSequence (SeName,Increment,CurVal) 
values ('DID',1,0) ;insert into TSysSequence (SeName,Increment,CurVal) values ('SID',1,0) ;
/*=======================================*//* Index: In_SeName *//*=======================================*/
create unique index In_SeName on TSysSequence (SeName ASC)
go

再创建一个存储过程完成序列的使用

if exists (select 1from sysobjectswhere id = object_id('PGetSequenceValue')and type in ('P','PC'))drop 
procedure PGetSequenceValuegocreate 
procedure PGetSequenceValue@SeName nvarchar(50),@SeVal bigint 
outasbeginif not 
exists(select 1 from TSysSequence where SeName = @SeName)
beginraiserror('不存在序列%s',16,1,@SeName)returnend
update TSysSequence set @SeVal = CurVal + Increment, 
CurVal = CurVal + Increment where SeName = @SeNameendgo

使用方法

declare @ID1 intEXEC PGetSequenceValue 'SID',@ID1 OUTPUT
select @ID1
declare @ID2 intEXEC PGetSequenceValue 'DID',@ID2 OUTPUT
select @ID2

下载本文
显示全文
专题