MSSqlServer pseudo sequence_mysql

Source: Internet
Author: User
Tags mssqlserver
MSSqlServer pseudo sequence MSSqlServer pseudo sequence_mysql

Create a sequence table 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 1 from 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. export 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 ','

Simulate oracle sequence

User maintenance is not allowed, and no one is allowed to modify the value after database initialization.

By default, two sequences named "DID" and "SID" are generated, meaning "data serial number" and "system serial number ". ',

'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

Create a stored procedure to complete sequence usage

If exists (select 1 from sysobjectswhere id = object_id ('pgetsequencevalue') and type in ('P', 'PC') drop procedure into procedure PGetSequenceValue @ SeName nvarchar (50 ), @ SeVal bigint outasbeginif not exists (select 1 from TSysSequence where SeName = @ SeName) beginraiserror ('Sequence % s' does not exist, 16,1, @ SeName) returnendupdate TSysSequence set @ SeVal = CurVal + Increment, CurVal = CurVal + Increment where SeName = @ SeNameendgo

Usage

declare @ID1 intEXEC PGetSequenceValue 'SID',@ID1 OUTPUTselect @ID1declare @ID2 intEXEC PGetSequenceValue 'DID',@ID2 OUTPUTselect @ID2

The above is the content of the MSSqlServer pseudo sequence_mysql. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.