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