Home > Database > Mysql Tutorial > 小系统单据自动生成存储过程

小系统单据自动生成存储过程

WBOY
Release: 2016-06-07 18:01:00
Original
808 people have browsed it

此处判断有两种方法:一种是根据传入6位日期判断;另一种根据单据创建日期字段(前提:表有创建时间字段)

代码如下:
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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template