This article introduces how to solve the maximum ID conflict in mssqlserver. If you need it, refer to it.
This article introduces how to solve the maximum ID conflict in mssqlserver. If you need it, refer to it.
Store the maximum Id in a specific table and retrieve the unique maximum value of each time by record.
| The Code is as follows: |
|
|
Create table [dbo]. [TbGuidTable] (
[TableName] [varchar] (50) not null,
[KeyName] [varchar] (20) not null,
[KeyValue] [varchar] (100) NOT NULL
) ON [PRIMARY]
GO
If exists (* from dbo. sysobjects where id = object_id (N 'pcreateguid') and OBJECTPROPERTY (id, n' IsProcedure ') = 1)
Drop procedure PCreateGuid
GO
------------------------------------
-- Purpose: Add a record of personal information
-- Project name:
-- Note:
-- Time: 15:34:12
------------------------------------
Create procedure PCreateGuid
@ TableName varchar (50 ),
@ Key name varchar (100 ),
@ Guid varchar (100) out
As
If not exists (select * from TbGuidTable where tablename = @ tableName and keyname = @ keyname)
Insert into TbGuidTable (tablename, keyname, keyvalue) values (@ tableName, @ keyname, 10000)
Update TbGuidTable set keyvalue = keyvalue + 1 where tablename = @ tableName and keyname = @ keyname
Select @ guid = cast (keyvalue as varchar) from TbGuidTable where tablename = @ tableName and keyname = @ keyname
Go
---*****---
Declare @ tableName varchar (50)
Declare @ keyname varchar (100)
Declare @ guid varchar (100)
Set @ tableName = 'tb1'
Set @ keyname = 'def'
Execute PCreateGuid @ tableName, @ keyname, @ guid out
Print @ guid |