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 1046 上升序列
Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ...
- BZOJ 1036 树的统计
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- Unity3d项目合作 场景的合并和还原
Unity3d项目合作 场景的合并和还原 特别声明:转载自Unity3D研究院 如何侵犯版权,请通知我删除! 摘要: 导出Unity场景的所有游戏对象信息,一种是XML一种是JSON.本篇文章我们把 ...
- 用STRACE解决公司真实故障一例
这是相关分析文档.为了职业操守,已修改相关公司敏感信息~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ 关于论坛每五分钟左右,会有warning.html跳转出现的原因调查 (warning. ...
- MySQL基本查询语句练习
努力很久只为获得别人尊重的眼光. ——我是,董宏宇,我为自己代言. 技术交流QQ:1358506549(请注明你的来意) use xsx; CREATE TABLE Course( Cno char( ...
- HDU 5452 Minimum Cut
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5452题目大意: 给你一个图G,图中包含一颗生成树.要求只能删除生成树内的一条边,使得图不联通.问最小的删除 ...
- HDU 5410 CRB and His Birthday
题目大意: 一个人要去买礼物,有M元.有N种礼物,每件礼物的价值是Wi, 你第i件礼物买k个 是可以得到 Ai * k + Bi 个糖果的. 问怎么才能使得你得到的糖果数目最多. 其实就是完全背包 ...
- 数学(线性规划): ZJOI2013 防守战线
偷懒用的线性规划. #include <iostream> #include <cstring> #include <cstdio> using namespace ...
- 使用DateAdd方法向指定日期添加一段时间间隔,使用TimeSpan对象获取时间间隔
一:使用DateAdd方法向指定日期添加一段时间间隔,截图 二:代码 using System; using System.Collections.Generic; using System.Comp ...
- UNIX环境下的消息队列
消息队列和共享内存一样,也是一种IPC对象.消息队列其实就是消息的链表,每一则消息都是用户自己的结构体.服务端这边创建消息队列,客户端这边打开消息队列,两个进程就可以进行通信.创建和打开消息队列使用函 ...