欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sqlserver数据库最大Id冲突问题解决方法之一

程序员文章站 2023-12-04 13:11:28
复制代码 代码如下: create table [dbo].[tbguidtable]( [tablename] [varchar](50) not null, [keyn...
复制代码 代码如下:

create table [dbo].[tbguidtable](
[tablename] [varchar](50) not null,
[keyname] [varchar](20) not null,
[keyvalue] [varchar](100) not null
) on [primary]

go

if exists (select * 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