订单号编码又修改后

来源:互联网 发布:画画美工钢笔推荐 编辑:程序博客网 时间:2024/04/28 04:35





---------------------------------------------------------------------------------------后又修改-------------------------------------------------------------------



--exec testlj 'A7','2013-8-21'
--创建人:lj
--订单评审的合同号
CREATE  proc  testlj
(@headStr nvarchar(10),@date datetime)
as
BEGIN 
declare  @oid2 nvarchar(50)
declare  @oid nvarchar(50)
declare @month nvarchar(2)
declare @year nvarchar(4)
declare @ym nvarchar(6)
declare @length  int 
set @month=month(@date)
if len(@month)=1
    set @month='0'+@month --使月为两位长
set @year=right(convert(nvarchar,year(@date)),2)
set @ym=@year+@month --组成年月字符
set @length =len(@headStr) 
--格式Q200908001
if exists(select * from htgl_base_No)
begin


    declare  @strsql  nvarchar(max)
   set @strsql=' select  top 1 @oid3=no from htgl_base_No   
    where  no like ''' + @headStr +'%''   order by id desc '
    


    exec sp_executesql @strsql,N'@oid3 nvarchar(50) output',@oid2 output  


    --获取最后一条的单据编号,一定要有id,并且自动生成的,倒排序
end
else 
begin
    set @oid2=@headStr+@ym+'00' --没有记录是默认为今天
end




--print  'oid2:  '+ @oid2
--订单不是本月的,重新开始一个新的订单流水号
if left(convert(nvarchar,right(@oid2,4)),2) <>@month
begin
--用本月的年月号开始
    set @oid2=@headStr+@ym+'00'
end


declare @str nvarchar(50) --临时单号


set @str=convert(nvarchar,(convert(int,right(@oid2,2))+1)) --订单号加一


--print 'str'+ @str
while (2-len(@str)>0)
begin
     set @str='0'+@str    
end
set @oid2=@headStr+@ym+@str
--print '---'+@oid2


--如果该订单好已经存在,则重新获取
while exists(select * from htgl_base_No where no=@oid2)  
begin
    
    set @str=convert(nvarchar,(convert(int,right(@oid2,2))+1)) --订单号加一
    while (2-len(@str)>0)
    begin
         set @str='0'+@str    
    end
    set @oid2=@headStr+@ym+@str
--     print @oid2
end


set @oid=convert(nvarchar,@oid2)
--print 'Q'+convert(nvarchar,year(getdate()))+convert(nvarchar,month(getdate()))+@str


select @oid
END