用存储过程生成记录编号CREATE procedure up_GetBeautifulOrderNumber(@orderNumber char(12) out)
用存储过程生成记录编号
as
用存储过程生成记录编号
begin
用存储过程生成记录编号
declare @number char(11)--编号的后11位
用存储过程生成记录编号
declare @maxDate decimal--从数据库表中当前最大的编号中取出的日期
用存储过程生成记录编号
declare @currentDate decimal    --当前日期
用存储过程生成记录编号
declare @currentNumber char(12--数据库表中当前最大的编号
用存储过程生成记录编号
declare @day char(2--当前日期,如果是一位数,需要将其转换为两位数
用存储过程生成记录编号
set @day=(select case len(datename(d,getdate())) 
用存储过程生成记录编号 
when 1 then '0'+ cast(datename(d,getdate()) as char(1))
用存储过程生成记录编号 
when 2 then cast(datename(d,getdate()) as char(2)) end)
用存储过程生成记录编号
set @currentDate=cast((datename(yyyy,getdate())+datename(mm,getdate())+ @dayas decimal)
用存储过程生成记录编号
if not exists(select top 1 OrderID from Orders order by OrderID desc)
用存储过程生成记录编号
begin
用存储过程生成记录编号 
set @number=cast((@currentDate *1000as char(11))
用存储过程生成记录编号
end
用存储过程生成记录编号
else 
用存储过程生成记录编号
begin
用存储过程生成记录编号  
set @currentNumber=(select top 1 OrderID from Orders order by OrderID desc)
用存储过程生成记录编号  
set @maxDate=cast(substring(@currentNumber2,8as decimal)
用存储过程生成记录编号  
if @maxDate<@currentDate 
用存储过程生成记录编号    
begin
用存储过程生成记录编号 
set @number=cast(@currentDate *1000 as char(11))
用存储过程生成记录编号    
end
用存储过程生成记录编号  
else if @maxDate=@currentDate
用存储过程生成记录编号    
begin
用存储过程生成记录编号 
set @number=cast((cast(substring(@currentNumber,2,12as decimal)+1as char(11))
用存储过程生成记录编号    
end
用存储过程生成记录编号  
else 
用存储过程生成记录编号    
begin
用存储过程生成记录编号 
print @maxDate
用存储过程生成记录编号 
declare @errMessage varchar(100)
用存储过程生成记录编号 
set @errMessage=cast(@maxDate as char(10))+ '错误,数据库表中纪录的最大日期有错误,请与数据库管理员联系。'
用存储过程生成记录编号 
raiserror(@errMessage161
用存储过程生成记录编号    
end
用存储过程生成记录编号
end
用存储过程生成记录编号   
set @orderNumber='B'+@number
用存储过程生成记录编号
end 

相关文章: