首页 > 代码库 > SQL生成流水号

SQL生成流水号

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 @numberEND

 

SQL生成流水号