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

制作淘宝订单流水号格式是:DD2001207150001
DD2001207150001 注:订单号的生成格式为:DDYYYYMMDD0001 到第二天的时候DDYYYYMMDD0001 它又要从0001开始加
这个怎么弄?

------解决方案--------------------
哎。

你可以创建一个叫做“编号”的表,例如;
编号(前缀,当前日期,当前编号)
然后每当新增一个订单,先到这个表中去将当前编号+1,再使用。

显然对于sql server而言它的事务隔离级别已经可以保证各个并行的数据库会话所得到的这个编号是唯一的了。
------解决方案--------------------
程序中判断一下:
if(当前的年月日>数据库中最后一个订单的年月日)
最后四位取0001
else
最后四位取数据库中当天最后一个订单的后四位+1
------解决方案--------------------
曾经用过的这样:
SQL code

--***************************************************
--创建日期:2010-09-6
--功能说明:生成单据编号
--参数说明:
--修改记录:
--***************************************************
CREATE PROCEDURE aa_createbillid
(    
    @iBillid         int,  
    @iPost         int,
    @strBillid     nvarchar(50) output 
)
as
set nocount on
declare @Date         varchar(20),
    @Year         varchar(20),
    @Year2        varchar(20),
    @Month     varchar(20),
    @Day         varchar(20),
    @BillCode     varchar(20),
    @BillWay     varchar(20),
    @SplitSign     varchar(20),
    @StartBill     varchar(20),
    @Temp     varchar(20),
    @strTmp     varchar(20),
    @SNextID     varchar(20)
declare    @MaxID         int,
    @BillID     int, 
     @iError     int,
    @iTmp         int,
     @flag         int

set @flag=0
set @Date = Convert(varchar(10),getdate(),120) 
set @Year = Year(@Date)
set @Year2 = Year(@Date)
set @Month = Month(@Date)
set @Day = Day(@Date)

set @Year=SUBSTRING(@Year,3,2)
if(len(@Month) = 1)
    set @Month = '0'+ @Month
if(len(@Day) = 1)
    set @Day = '0'+ @Day
if not exists(select * from NOPlan where [ID]=@iBillid)
begin
    execute aa_addbillid @iBillid     
end
select @BillCode=Code,@BillWay=Style,@SplitSign=Tally,@StartBill=BeginNO from NOPlan where [ID]=@iBillid
if(@BillWay = '年')
begin
    set @Temp = @Year
    if exists(select * from NOList where datepart(yy,_Date)= @Year2 and [PlanID]=@iBillid) 
    begin
        select @MaxID = MaxValue ,@BillID=[ID] from NOList where  datepart(yy,_Date)= @Year2 and [PlanID]=@iBillid  
        set @flag=1
    end
end 
else if(@BillWay = '年月' or @BillWay = '月年')
begin
    if(@BillWay = '年月')
        set @Temp=@Year+@Month
    else if(@BillWay = '月年')
        set @Temp = @Month+@Year
    if exists(select * from NOList where datepart(yy,_Date)= @Year2 and datepart(mm,_Date)=@Month and [PlanID]=@iBillid)
    begin
        select @MaxID = MaxValue ,@BillID=[ID] from NOList where  datepart(yy,_Date)= @Year2 and datepart(mm,_Date)=@Month and [PlanID]=@iBillid 
        set @flag=1
    end
end
else if(@BillWay = '年月日' or @BillWay = '月日年')
begin
    if(@BillWay = '年月日')
        set @Temp = @Year+@Month+@Day
    else if(@BillWay = '月日年')
        set @Temp = @Month+@Day+@Year
    if exists(select * from NOList where _Date=@Date and [PlanID]=@iBillid)
    begin
        select @MaxID=MaxValue,@BillID=[ID] from NOList where  _Date=@Date and [PlanID]=@iBillid 
        set @flag=1
    end
end
else
begin
    set @Temp = ''
    if exists(select * from NOList where  [PlanID]=@iBillid)
    begin
        select @MaxID=MaxValue,@BillID=[ID] from NOList where [PlanID]=@iBillid
        set @flag=1
    end
end
if(@flag=1)
begin
    set @MaxID = @MaxID + 1
    set @SNextID = @MaxID
    --得到起始编号的最大值
    set @strTmp='9'
    while(len(@strTmp)<len(@StartBill))
        begin
        set @strTmp='9' + @strTmp
        end
    set @iTmp=convert(int,@strTmp)+1
    --判断越界
    if @iTmp=@MaxID 
    begin
        set @StartBill='0' + @StartBill
        update NOPlan set BeginNO=@StartBill where [ID]=@iBillid
    end
    --得到当前流水号
    while(len(@SNextID)<len(@StartBill))
        begin
        set @SNextID='0' + @SNextID
        end
    --去除自定义多余的分隔符
    if @Temp=''
        set @strBillid=@BillCode+@Temp+@SplitSign+@SNextID
    else
        set @strBillid=@BillCode+@SplitSign+@Temp+@SplitSign+@SN