国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

SQL 生成可配置流水號(hào)

2024-07-21 02:50:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
SQL 生成可配置流水號(hào)

需求背景每執(zhí)行一次方法,根據(jù)公式返回最新的流水號(hào)。第一次使用時(shí)需要先插入一條數(shù)據(jù),BizSeqValue 為流水起始號(hào):A2014030000,F(xiàn)ormula 為公式:A[yyyy][mm][c4],UseTime 為當(dāng)前時(shí)間。

創(chuàng)建流水號(hào)表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)
創(chuàng)建PadLeft 函數(shù)Create function [dbo].[PadLeft](@num varchar(16),@paddingChar char(1),@totalWidth int)returns varchar(16) asbeginif(len(@num)=0)begin    return ''enddeclare @curStr varchar(16)select @curStr = isnull(replicate(@paddingChar,@totalWidth - len(isnull(@num ,0))), '') + @numreturn @curStrend
Create PROCEDURE [dbo].[Biz_GetSeqNo]    @BizSeqType varchar(50)ASBEGIN    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=@BizSeqTypeselect @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())endelse if(charindex('[yy]',@Formula)>0)begin    select @Yearindex=charindex('[yy]',@Formula)    select @year=SUBSTRING(CAST(YEAR(getdate()) as varCHAR(4)),3,2)endselect @monthindex=charindex('[mm]',@Formula)if(@monthindex>0)begin    select @month=month(getdate())endselect @dayindex=charindex('[dd]',@Formula)if(@dayindex>0)begin    select @day=day(getdate())endselect @Half1=@Prefix+@Year+dbo.PadLeft(@Month,'0',2)+dbo.PadLeft(@Day,'0',2)--select @Half1,@Prefix,@Yearif(@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-6endelse 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-4endelse 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-2endelsebegin    select @Half2=SUBSTRING(@Formula,LEN(@Prefix)+1,999)    select @Count=COUNT(1) from SM_BizSeqNo where BizSeqType=@BizSeqTypeendif(@Count>0) --當(dāng)前流水號(hào)+1begin    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=@BizSeqTypeendelsebegin    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=@BizSeqTypeendselect BizSeqValue from SM_BizSeqNo where BizSeqType=@BizSeqTypeEND


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三亚市| 峨山| 阿鲁科尔沁旗| 昭平县| 慈溪市| 崇义县| 城市| 美姑县| 宜州市| 临漳县| 江永县| 乌兰浩特市| 武乡县| 台中县| 伊吾县| 双鸭山市| 都江堰市| 普兰店市| 宜昌市| 西安市| 中超| 天祝| 贡山| 鹤山市| 北海市| 沾益县| 台前县| 育儿| 惠州市| 松桃| 宁阳县| 抚顺县| 澄江县| 临漳县| 嘉定区| 通辽市| 明星| 江北区| 突泉县| 六安市| 永泰县|