select
流水号 = right('000'+rtrim(isnull(max(cast(流水号 as int)),0)+1),4)
from
表
where
datediff(day,日期,getdate()) = 0
-------------------------------------------
同意,这样可行
CREATE Proc p_Getpaperno
@head varchar(3) = 'In'
AS
/*
編碼原則---- 單據頭(2位)+年(2位)+月(2位)+流水碼(3位)
*/
Declare @year varchar(2),@month varchar(2),@i varchar(3),@paperno varchar(14)
Select @year = Right(datepart(year,Getdate()),2) --年
Select @month = [month ] from papercode where paperhead = @head
if @month <> Convert(varchar(2),datepart(month,Getdate())) --月
begin
Update PaperCode
set [month] = Convert(varchar(2),datepart(month,Getdate()))
Select @month = Convert(varchar(2),datepart(month,Getdate()))
end
if len(@month) = 1
set @month = '0' + @month
Select @i = papercode from papercode where paperhead = @head
if len(@i) = 1
set @i = '00' + @i
if len(@i) = 2
set @i = '0' + @i
Update PaperCode
Set papercode = papercode + 1,Tpaperno = @head + @year + @month + @i
where paperhead = @head
select Tpaperno from PaperCode where paperhead = @head
到月了,到天的自己改