复制代码 代码如下:
create table [order]
(
code varchar(50),
createtime datetime
)
–应用 usp_ordernumbergenerate(@prefix = ‘PRC100701’)
–传入前缀 大类+单据编码+6位日期
–获取当日该类单据最大流水号(需按日归零)
–此处判断有两种方法:一种是根据传入6位日期判断;另一种根据单据创建日期字段(前提:表有创建时间字段)
create procedure usp_OrderNumberGenerate
@prefix varchar(50)
as
declare @count int
declare @midcode varchar(3)
declare @Digits int = 3
declare @orderNumber varchar(50)
select @count = COUNT(*) from [order] where DATEDIFF(day, createtime, GETDATE()) = 0
if(@count = 0) –当日无单据情况 流水号为001
–print @count
select @orderNumber = @prefix + ‘001’
else –当日有单据情况 最大流水号+1
select @midcode = max(substring(midcode, 10, 3)) + 1
from [order]
where DATEDIFF(day, createtime, GETDATE()) = 0
select @orderNumber =@prefix + RIGHT(REPLICATE(‘0’, @Digits)
+ CAST(@midcode as VARCHAR), @Digits)
print @ordernumber