需求背景

每执行一次方法,根据公式返回最新的流水号。
第一次使用时需要先插入一条数据,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 生成可配置流水号的更多相关文章

  1. oracle instantclient basic +pl/sql 安装和配置

    oracle instantclient basic +pl/sql 安装和配置 大家都知道,用PL/SQL连接Oracle,是需要安装Oracle客户端软件的,oracle客户端有点大,比较耗资源. ...

  2. mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

    mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置 ============================== 蕃薯耀 2018年3月14 ...

  3. Oracle 11g XE 与 Oracle SQL Developer 的配置与使用(重制版)

    Oracle 11g XE 与 Oracle SQL Developer 的配置与使用(重制版) 前提概要 项目上需求要适应Oracle数据库,当然这和某EF框架也有关. 因为Oracle 的表名和列 ...

  4. 使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置

    1.使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置 2.上代码:在resources目录下新建:generatorConfiguration.xml文 ...

  5. PL/SQL安装部署配置(配图解)

    PL/SQL安装部署配置 下载好安装包之后,双击exe程序 双击安装程序,出现如下页面 点击[NEXT],出现如下界面 选择[I Accept...],点击[NEXT],出现如下界面 选择安装路径,点 ...

  6. SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件

    SQL SERVER 2008配置Database Mail –用SQL  数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...

  7. Oracle 客户端安装 + pl/sql工具安装配置

    Oracle 客户端安装 +  pl/sql工具安装配置 下载oracle客户端,并在本地安装. 11g下载地址为: http://www.oracle.com/technetwork/databas ...

  8. c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”

    ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述     相关代码 using System; us ...

  9. sql 生成excel

    1创建存储过程 create PROCEDURE [dbo].[UP_Export_Data_To_CSV] @Table NVARCHAR(), @Path NVARCHAR(), @Subfix ...

随机推荐

  1. Dao 处理

    1. 写一个基础的接口和类来做基本的操作 /** * */ package com.wolfgang.dao; import java.util.List; /** * @author Adminis ...

  2. Node.js Express框架

    Express 介绍 Express是一个最小的,灵活的Node.js Web应用程序框架,它提供了一套强大的功能来开发Web和移动应用程序. 它有助于基于Node Web应用程序的快速开发.下面是一 ...

  3. [BZOJ 1068] [SCOI2007] 压缩 【记忆化搜索】

    题目链接:BZOJ - 1068 题目分析 这种记忆化搜索(区间 DP) 之前就做过类似的,也是字符串压缩问题,不过这道题稍微复杂一些. 需要注意如果某一段是 S1S1 重复,那么可以变成 M + S ...

  4. linux在shell中获取时间

    linux在shell中获取时间 获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 将当前日期赋值给DATE变量DATE=$(date +%Y%m%d) 有时候我们需要使用今 ...

  5. The top 100 papers Nature explores the most-cited research of all time.

    The top 100 papers Nature explores the most-cited research of all time. The discovery of high-temper ...

  6. 汉字转拼音的Java类库:JPinyin

    JPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进. [JPinyin主要特性]1.准确.完善的字库:Unicode编码从4E00-9FA5范围及3007(〇 ...

  7. 【动态规划】HDU 5791 Two

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5791 题目大意: A,B两个数列,问A的子集和B的子集相等的子集对数.子集内顺序按照数列顺序,相同的 ...

  8. Java---实现运行任意目录下class中加了@MyTest的空参方法(实现图形界面)

    说明: 因为上个代码,总是要输入完整的绝对路径,比较麻烦,于是,就写了这个小程序,直接进入文件对话框选择需要运行的class文件. 只需要提前输入完整的类名. 注意:加的MyTest必须打个包,加上: ...

  9. poj1009

    题目大意:边缘检测 IONU卫星成像公司,公司记录和储存非常大的图片使用行程编码,你需要编写一个程序读取被压缩的图像,发现图像的边缘,描述如下,并且输出另一个发现的边缘压缩图像. 一个简单的边缘检测算 ...

  10. easyUI属性总结

    1.div easyui-window        生成一个window窗口样式.      属性如下:                   1)modal:是否生成模态窗口.true[是] fal ...