create table UniqueID(fDate char(8),fSerialNo int)
go
create procedure GetUniqueID(@ID varchar(20) output)
as
declare @strDate char(8)
set @strDate=convert(char(8),getdate(),112)
if (not exists(select 1 from UniqueID where fDate=@strDate))
insert into UniqueID (fDate,fSerialNo) values(@strDate,0)
update UniqueID set fSerialNo=fSerialNo+1 where fDate=@strDate
select @ID=fDate+convert(varchar(20),fSerialNo) from UniqueID where fDate=@strDate
go
create table UniqueID(fDate char(8),fSerialNo int)
go
create procedure GetUniqueID(@ID varchar(20) output)
as
declare @strDate char(8)
set @strDate=convert(char(8),getdate(),112)
if (not exists(select 1 from UniqueID where fDate=@strDate))
insert into UniqueID (fDate,fSerialNo) values(@strDate,0)
update UniqueID set fSerialNo=fSerialNo+1 where fDate=@strDate
select @ID=fDate+convert(varchar(20),fSerialNo) from UniqueID where fDate=@strDate
go
--创建一个取编号的存储过程
create proc p_getbh
@编号 varchar(10) out
as
declare @a rowversion
select @编号=编号+1,@a=t from tb
update tb set 编号=@编号 where t=@a
if @@rowcount=0
raiserror('编号被抢占',1,16)
go
--调用存储过程取编号
declare @编号 varchar(10)
exec p_getbh @编号 out
select @编号
go