Home > Database > Mysql Tutorial > body text

mssqlserver中最大ID冲突解决办法

WBOY
Release: 2016-06-07 17:48:48
Original
1067 people have browsed it

本文章介绍了关于mssqlserver中最大ID冲突解决办法,有需要的朋友可以参考一下。

在一个特定的表中保存最大Id,通过记录来取每次的唯一最大值。

 代码如下 复制代码

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
------------------------------------
--用途:增加一条记录  个人信息
--项目名称:
--说明:
--时间:2011-7-4 15:34:12,2011-08-15
------------------------------------
CREATE PROCEDURE PCreateGuid
@tableName varchar(50),
@keyname 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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template