根据一定原则自动生成序列号的具体示例
需求:
◆1、生产收货时,在数量处按CTRL+TAB,在序列号录入处按ShiFT+F2,能自动生成序列号;
◆2、生成原则如下:0703MG51T-09-S05-1200#001,其中0703为年月代码,MG51T-09-S05-1200为物料号,#号为分隔符,001为本月第一笔收货,以后自动加1。
我的语句如下:
declare @t1 as varchar(32)
declare @t2 as varchar(32)
declare @itemcode as varchar(20)
declare @y as varchar(20)
declare @m as varchar(20)
declare @ml as integer
declare @qty1 as varchar(3)
declare @maxid as varchar(3)
declare @tid as varchar(3)
select @itemcode=rtrim($[$43.5.0])
select @y=substring(cast(year(getdate()) as varchar),3,2)
select @m=cast(month(getdate()) as varchar)
select @ml=len(Rtrim(@m))
if @ml=1
select @m='0'+@m
else select @m=@m
select @t1=Ltrim(@y+@m+@itemcode)
select @qty1=isnull(count(*),0) from OSRI where ItemCode=@itemcode and substring(IntrSerial,1,4)=@y+@m
if Rtrim(@qty1)='0'
begin
select @tid='001'
end
else
begin
select @maxid=cast(isnull(max(cast(substring(IntrSerial,len(Rtrim(IntrSerial))-2,3) as float)),0) as float)+1 from OSRI where ItemCode=Rtrim(@itemcode) and substring(IntrSerial,1,4)=@y+@m
if len(Rtrim(@maxid))=0
begin
select @tid='001'
end
if len(Rtrim(@maxid))=1
begin
select @tid='00'+cast(Rtrim(@maxid) as char(1))
end
if len(Rtrim(@maxid))=2
begin
select @tid='0'+cast(Rtrim(@maxid) as char(2))
end
if len(Rtrim(@maxid))=3
begin
select @tid=cast(Rtrim(@maxid) as char(3))
end
end
select @t2=@t1+'#'+@tid
select @t2