How does SQL Server ensure the uniqueness of global ID numbers in concurrent scenarios?

Source: Internet
Author: User

A stored procedure is used to generate a unique ID and ensure the uniqueness of the ID number in the case of concurrency.

It can be used in the database system to obtain the globally unique sequence number.

 

Run the following code for SQL Server 2000:

Create procedure getnewid

Declare
@ ID int;

Begin transaction
Insert into sysnewid (tmpint) values (1 );
Set @ ID = ident_current ('sysnewid ')
Commit transaction

Return @ ID
Go

/* Function description, from the Help File of SQL Server 2000

Syntax ident_current (' Table_name ')Parameters

Table_name

Is the name of the table whose ID value will be returned.Table_nameThe data type of isVarchar, No default value.

 

Ident_current returns the last generated id value for any session and specific tables in any scope.
@ Identity returns the last generated id value for any table in all scopes of the current session.
Scope_identity returns the last generated id value for the current session and any table in the current scope.

*/

 

// Data table that generates IDS

Create Table [DBO]. [sysnewid] (
[ID] [int] identity (1, 1) not null, // automatically add a field.
[Tmpint] [int] Null
) On [primary]

 

/* Query the test code in the analyzer */

Declare @ ID int
Exec @ ID = getnewid
Print @ ID

 

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.