create function NextOrderNumber()
returns char(8)
as
begin
declare @lastval char(8)
declare @curyear char(4)
set @curyear = 'OR' + (select right(YEAR(getdate()),2))
set @lastval = (select MAX(orderNumber) from [PROJ].[dbo].[ZTemp001] where orderNumber like '%' + @curyear + '%')
if @lastval is null set @lastval = (@curyear + '0000')
declare @i int
set @i = convert(integer,RIGHT(@lastval,4)) + 1
return @curyear + right('000' + convert(varchar(10),@i),4)
end