Create function [dbo].[f_GetFxNum]()
returns varchar(15)
as
begin
declare @FxNum varchar(15)
declare @time varchar(8)
set @time=CONVERT(varchar,YEAR(GETDATE()))+RIGHT('00'+CONVERT(varchar,month(getdate())),2)+RIGHT('00'+CONVERT(varchar,DAY(getdate())),2)--取到当前年月日
select @FxNum=@time+RIGHT(100000+ISNULL(RIGHT(MAX(SerialNo),5),0)+1 ,5)--获取当前表最大值并加1
from BC_OutQAReport where month(CreateDate)=MONTH(GETDATE())
return @FxNum
end
为字段绑定默认值:
ALTER TABLE BC_OutQAReport ADD DEFAULT ([dbo].[f_GetFxNum]()) FOR SerialNo --将该函数绑定到SerialNo作为该字段的默认值