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,'',2)+dbo.PadLeft(@Day,'',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,'',@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('','',@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
SQL 生成可配置流水号的更多相关文章
- oracle instantclient basic +pl/sql 安装和配置
oracle instantclient basic +pl/sql 安装和配置 大家都知道,用PL/SQL连接Oracle,是需要安装Oracle客户端软件的,oracle客户端有点大,比较耗资源. ...
- mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置
mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置 ============================== 蕃薯耀 2018年3月14 ...
- Oracle 11g XE 与 Oracle SQL Developer 的配置与使用(重制版)
Oracle 11g XE 与 Oracle SQL Developer 的配置与使用(重制版) 前提概要 项目上需求要适应Oracle数据库,当然这和某EF框架也有关. 因为Oracle 的表名和列 ...
- 使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置
1.使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置 2.上代码:在resources目录下新建:generatorConfiguration.xml文 ...
- PL/SQL安装部署配置(配图解)
PL/SQL安装部署配置 下载好安装包之后,双击exe程序 双击安装程序,出现如下页面 点击[NEXT],出现如下界面 选择[I Accept...],点击[NEXT],出现如下界面 选择安装路径,点 ...
- SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件
SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...
- Oracle 客户端安装 + pl/sql工具安装配置
Oracle 客户端安装 + pl/sql工具安装配置 下载oracle客户端,并在本地安装. 11g下载地址为: http://www.oracle.com/technetwork/databas ...
- c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”
ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述 相关代码 using System; us ...
- sql 生成excel
1创建存储过程 create PROCEDURE [dbo].[UP_Export_Data_To_CSV] @Table NVARCHAR(), @Path NVARCHAR(), @Subfix ...
随机推荐
- BZOJ 1031 字符加密
Description 喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法.例如下图,可以读作 ...
- [BZOJ 2724] [Violet 6] 蒲公英 【分块】
题目链接:BZOJ - 2724 题目分析 这道题和 BZOJ-2821 作诗 那道题几乎是一样的,就是直接分块,每块大小 sqrt(n) ,然后将数字按照数值为第一关键字,位置为第二关键字排序,方便 ...
- IIC协议及其对ACK应答信号的处理
1,SCL一直由Master控制,SDA依照数据传送的方向,读数据时由Slave控制SDA,写数据时由Master控制SDA.当8位数据传送完毕之后,应答位或者否应答位的SDA控制权与数据位传送时相反 ...
- oracle中的exists 和not exists 用法详解(转)
有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...
- Windows下Vundle插件BundleSearch命令出现错误解决方案
纯粹参考他人博文,已证明解决方案正确,转载时请注明参考链接!!! 参考链接: http://www.tuicool.com/articles/e2aiEn 在Windows下使用Vim时,用Vundl ...
- [LeetCode#136, 137]Single Number, Single Number 2
The question: Single Number Given an array of integers, every element appears twice except for one. ...
- ZOJ-2008-Invitation Cards(dijkstra)
题意: 在有向加权图中G(V,E),邮局要从起点S向其他n个节点发送邮件,于是派出n个邮递员,分别到达其他n个地点发送,然后回到起点S,求出所有邮递员所经过的总路程的最小值. 分析: 正向一次dijk ...
- Linux Shell编程(7)——变量赋值
=赋值操作符(它的左右两边不能有空白符) 不要搞混了=和-eq,-eq是比赋值操作更高级的测试.注意:等于号(=)根据环境的不同它可能是赋值操作符也可能是一个测试操作符.例子:简单的变量赋值 #!/b ...
- Linux Shell编程(2)——第一个shell程序
在最简单的情况下,脚本程序不过是存储在一个文件里的系统命令列表.这至少让你执行它 时不必重新按顺序键入相同功能的命令序列.一个清空/var/log目录下的日志文件的脚本 # Cleanup # 必须以 ...
- Missing Number ——LeetCode
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...