码迷,mamicode.com
首页 > 数据库 > 详细

SQL生成流水号

时间:2015-01-05 10:56:40      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

BEGIN
    SET NOCOUNT ON;
     dECLARE @number VARCHAR(100)
     declare @sdate datetime
    declare @id VARCHAR(32)
    set @id=0
    SET @number=‘‘
    select @id=id,@sdate=sdate from Tb_AccountsSeedcode where [type]=@type
   PRINT @id
    if(@id is NULL  OR @id=0)
    BEGIN
        DECLARE @guid VARCHAR(36);
        SELECT @guid=NEWID()
        insert into Tb_AccountsSeedcode([Id],[Creator],[CreateTime],[Modifier],[ModifyTime],[Type],[Number],[Sdate])
        values(REPLACE(@guid,-,‘‘),@userid,getdate(),NULL,NULL,@type,1,getdate()) 
    END
    ELSE
    BEGIN
        if(DATEDIFF(day,@sdate,GETDATE())<>0)
        BEGIN
            update Tb_AccountsSeedcode set Number=1,Sdate=GETDATE() where [TYPE]=@type
            
        END
        ELSE
        BEGIN    
            update Tb_AccountsSeedcode set Number=Number+1 where [TYPE]=@type
        END
    END    
    
    select @number=Se.[Type]+Replace(CONVERT(varchar(10),getdate(),120),-,‘‘)+
    Replace(space(5-len(se.Number)), ,0)+cast(se.Number as varchar) from Tb_AccountsSeedcode se with(nolock)
    where se.[type]=@type 
    SELECT @number
END

 

SQL生成流水号

标签:

原文地址:http://www.cnblogs.com/yufan27209/p/4202745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!