SQL Server数据库中用存储过程来取顺序号
表sys_number,字段name字符,表示前缀,value数字,一个一个递增
存储过程代码:
存储过程代码:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[usp_GetSysid]@name varchar(50),@initvalue int,@id int output as /*===获取各种编号====*/ --开始事务 begin tran tr --判断是否存在 if not exists(select * from sys_number where name=@name )begininsert into Sys_Number(name,value) values(@name,@initvalue)end --取值 select @id=value from sys_number with(XLOCK) where name=@name if @id<@initvalue set @id=@initvalue --更新值 update sys_number set value=@id+1 where name=@name --and value=@id--提交事务 if @@error!=0 beginrollback tran trselect 0 end else begincommit tran trselect @id end
使用:
DECLARE @return_value int,@id intEXEC @return_value = [dbo].[usp_GetSysid]@name = N'GH20250304', --前缀@initvalue = 1, --初始值@id = @id OUTPUT --返回顺序号,前台程序可以拼接上name