SQL 生成可配置流水号

SQL 生成可配置流水号

需求背景

每执行一次方法,根据公式返回最新的流水号。
第一次使用时需要先插入一条数据,BizSeqValue 为流水起始号:A2014030000,Formula 为公式:A[yyyy][mm][c4],UseTime 为当前时间。

复制代码
创建流水号表
CREATE
TABLE [dbo].[SM_BizSeqNo]( [BizSeqID] [int] IDENTITY(1,1) NOT NULL,
[BizSeqValue][nvarchar](50) NULL,
[BizSeqName] [nvarchar](50) NULL,[UseTime] [datetime] NULL, [Formula] [varchar](50) NULL)
复制代码
复制代码
创建PadLeft 函数
Create function [dbo].[PadLeft](@num varchar(16),@paddingChar char(1),@totalWidth int) returns varchar(16) as begin if(len(@num)=0) begin return '' end declare @curStr varchar(16) select @curStr = isnull(replicate(@paddingChar,@totalWidth - len(isnull(@num ,0))), '') + @num return @curStr end
复制代码
复制代码
Create PROCEDURE [dbo].[Biz_GetSeqNo]
    @BizSeqType varchar(50)
AS
BEGIN
    declare
@BizSeqValue varchar(50),
@Prefix varchar(10),
@Year varchar(4),
@Yearindex int,
@Month varchar(2),
@Monthindex int,
@Day varchar(2),
@Dayindex int,
@DigitsIndex int,
@DigitsEndIndex int,
@Digits int,
@FlowNum varchar(50),
@Half1 varchar(50),
@Half2 varchar(50),
@Count int,
@Formula varchar(50)

set @Year='' set @Month='' set @Day=''
select @Formula=Formula,@BizSeqValue=BizSeqValue from SM_BizSeqNo where BizSeqType=@BizSeqType
select @Prefix=SUBSTRING(@Formula,0,charindex('[',@Formula))
select @DigitsIndex=charindex('[c',@Formula)
select @DigitsEndIndex=charindex(']',@Formula,@DigitsIndex+2)
select @Digits=SUBSTRING(@Formula,@DigitsIndex+2,@DigitsEndIndex-@DigitsIndex-2)
select @Yearindex=charindex('[yyyy]',@Formula)
if(@Yearindex>0)
begin
    select @year=YEAR(getdate())
end
else if(charindex('[yy]',@Formula)>0)
begin
    select @Yearindex=charindex('[yy]',@Formula)
    select @year=SUBSTRING(CAST(YEAR(getdate()) as varCHAR(4)),3,2)
end
select @monthindex=charindex('[mm]',@Formula)
if(@monthindex>0)
begin
    select @month=month(getdate())
end
select @dayindex=charindex('[dd]',@Formula)
if(@dayindex>0)
begin
    select @day=day(getdate())
end
select @Half1=@Prefix+@Year+dbo.PadLeft(@Month,'0',2)+dbo.PadLeft(@Day,'0',2)
--select @Half1,@Prefix,@Year
if(@Dayindex>0)
begin
    select @Half2=SUBSTRING(@Formula,@Dayindex+4,999)
    select @Count=COUNT(1) from SM_BizSeqNo where BizSeqType=@BizSeqType and DATEDIFF(DAY,UseTime,GETDATE())=0 
    select @DigitsIndex=@DigitsIndex-6
end
else if(@Monthindex>0)
begin
    select @Half2=SUBSTRING(@Formula,@Monthindex+4,999)
    select @Count=COUNT(1) from SM_BizSeqNo where BizSeqType=@BizSeqType and DATEDIFF(MONTH,UseTime,GETDATE())=0 
    select @DigitsIndex=@DigitsIndex-4
end
else if(@Yearindex>0)
begin
    select @Half2=SUBSTRING(@Formula,@Yearindex+2+LEN(@year),999)
    select @Count=COUNT(1) from SM_BizSeqNo where BizSeqType=@BizSeqType and DATEDIFF(YEAR,UseTime,GETDATE())=0 
    select @DigitsIndex=@DigitsIndex-2
end
else
begin
    select @Half2=SUBSTRING(@Formula,LEN(@Prefix)+1,999)
    select @Count=COUNT(1) from SM_BizSeqNo where BizSeqType=@BizSeqType
end
if(@Count>0) --当前流水号+1
begin
    select @FlowNum=cast(SUBSTRING(@BizSeqValue,@DigitsIndex,@Digits) as bigint)+1
    select @FlowNum=dbo.PadLeft(@FlowNum,'0',@Digits)
    update SM_BizSeqNo set BizSeqValue=@Half1+REPLACE(@Half2,'[c'+cast(@Digits as varchar(50))+']',@FlowNum),
    UseTime = GETDATE() where BizSeqType=@BizSeqType
end
else
begin
    select @FlowNum=dbo.PadLeft('1','0',@Digits)
    update SM_BizSeqNo set BizSeqValue=@Half1+REPLACE(@Half2,'[c'+cast(@Digits as varchar(50))+']',@FlowNum),
    UseTime = GETDATE() where BizSeqType=@BizSeqType
end
select BizSeqValue from SM_BizSeqNo where BizSeqType=@BizSeqType
END
复制代码

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值