制作淘宝订单流水号格式是: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 )asset nocount ondeclare @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 intset @flag=0set @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'+ @Monthif(len(@Day) = 1) set @Day = '0'+ @Dayif not exists(select * from NOPlan where [ID]=@iBillid)begin execute aa_addbillid @iBillid endselect @BillCode=Code,@BillWay=Style,@SplitSign=Tally,@StartBill=BeginNO from NOPlan where [ID]=@iBillidif(@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 endend 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 endendelse 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 endendelsebegin set @Temp = '' if exists(select * from NOList where [PlanID]=@iBillid) begin select @MaxID=MaxValue,@BillID=[ID] from NOList where [PlanID]=@iBillid set @flag=1 endendif(@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+@SNextID if @iPost=1 begin update NOList set MaxValue=@MaxID where [ID]=@BillID--更新最大键值 end endelsebegin set @MaxID = 1 set @SNextID = @MaxID 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+@SNextID if @iPost=1 begin insert into NOList(_Date,[PlanID],MaxValue) values(@Date ,@iBillid,@MaxID) endendreturn 0GO
[解决办法]
这个东西最好充分利用服务来解决,而不要动不动去查数据库的最大值。对于几乎是调用最频繁的业务逻辑,我们有必要充分优化,能在内存中消化的就在内存中解决。
做一个生成序列号的服务,我做过一个类似的,自以为还是比较巧妙:
- C# code
static object obj = new object(); const string orderFormat = "S{0:yyMMdd}{1:000}"; static string maxOrder = string.Format(orderFormat, DateTime.Today, 0); public static string GetUniqueOrderNo() { int orderIndex = 0; lock (obj) { DateTime dateKey = DateTime.Today; if (maxOrder == string.Format(orderFormat, dateKey, 0)) { maxOrder = CommOp.ToStr(DBHelper.DefaultHelper.ExecGetObject(String.Format("SELECT MAX(OrderNo) FROM B_Order WHERE OrderNo>='{0}'", maxOrder))); } if (!String.IsNullOrEmpty(maxOrder)) maxOrder = maxOrder.Substring(maxOrder.Length - 3); orderIndex = CommOp.ToInt(maxOrder); maxOrder = string.Format(orderFormat, dateKey, ++orderIndex); return maxOrder; } }