日期:2014-05-17  浏览次数:20470 次

自动生成自定义序号的存储过程有问题,随机会出现重复序号,请大家帮忙给看看,感激不尽。
--获取id    Id的格式为201312280001-201312289999
CREATE PROCEDURE prBuyMakeNewBuyID
@strToday varchar(50)  --唯一的参数 格式为yyyymmdd,例如20131228   20140101
AS
set transaction isolation level serializable  --加上最严格的隔离锁
begin transaction
    declare @maxid varchar(50)                --需要返回的序号
    declare @tmpnum float                     --临时号码

    select @tmpnum=convert(float,right(isnull(max(id), @strToday+'0000'),4)) from vwbuybuys where left(id,8)=@strToday   --如果当日最大号码为20131228000,则临时号码为0 如果当日最大号码为
--201312280011,则临时号码为11
    select @tmpnum=@tmpnum+1   --给临时号码加1
    if @tmpnum<=9 and @tmpnum>=0
        select @maxid=@strToday + '000' + convert(varchar(50),@tmpnum)
    if @tmpnum<=99 and @tmpnum>=10
        select @maxid=@strToday + '00' + convert(varchar(50),@tmpnum)        
    if @tmpnum<=999 and @tmpnum>=100
        select @maxid=@strToday + '0' + convert(varchar(50),@tmpnum)
    if @tmpnum<=9999 and @tmpnum>=1000
        select @maxid=@strToday +  convert(varchar(50),@tmpnum)
    --将临时号码转换为4位,不够四位就前面加0   ,保证号码为四位
    --然后将进来的唯一参数与转换后临时号码 组合到一起

    declare @province varchar(100)
    declare @city varchar(100)
    declare @county varchar(100)
    declare @company varchar(100)
    declare @allownowsubweight int    
    select @province=province,@city=city,@county=county, @company=company from tbcommcompany
    select @allownowsubweight=allownowsubweight from tbBuyControlArgs
    
    if @allownowsubweight=1
        begin
            insert into tbbuybuys (id,inlock,assaylock,totalweightlock,carweightlock,nowsubweightlock,calclock, province,city,county1,company,carweightmicrochange,isup,othermark) values(@maxid,0,0,0,0,0,0,@province,@city,@county,@company,0,'未上传',0)
        end
    else
        begin
            insert into tbbuybuys (id,inlock,assaylock,totalweightlock,carweightlock,nowsubweightlock,calclock,province,city,county1,company,carweightmicrochange,isup,harlamount1,harlweight,meshamount1,meshweight,packsubweight,nowsubweight,othermark) values(@maxid,0,0,0,0,1,0,@province,@city,@county,@company,0,'未上传',0,0,0,0,0,0,0)
        end
    select @maxid