1 CREATE FUNCTION GetMaxbhInc (@bh varchar(50)) 2 RETURNS varchar(50) 3 AS 4 BEGIN 5 declare @return varchar(50),@i int,@len int,@v int,@vs varchar(10) 6 7 set @bh=isnull(@bh,'00') 8 9 set @len=len(@bh) 10 11 set @i=patindex('%[0-9]%',@bh) 12 if @i=0 13 begin 14 set @return=@bh+'1' 15 end 16 else 17 begin 18 set @len=@len-@i+1 19 set @v=cast(right(@bh,@len) as int )+1 20 set @vs=cast(@v as varchar) 21 if @len<len(@vs) 22 set @len=len(@vs) 23 24 set @return=left(@bh,@i-1)+right('0000000000000000'+@vs,@len) 25 end 26 27 28 return @return 29 END