How to solve the maximum ID conflict in mssqlserver

Source: Internet
Author: User
Tags mssqlserver
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

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.