Microsoft SQL Server: Generate a sequence number, per day

后端 未结 5 1936
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 12:22

I\'m tasked to create an increasing sequence number per day for a project. Multiple processes (theoretically on multiple machines) need to generate this. It ends up as

5条回答
  •  独厮守ぢ
    2021-01-06 12:57

    I tried this way to create session codes for user logging and its working;

    CREATE FUNCTION [dbo].[GetSessionSeqCode]()
    RETURNS VARCHAR(15) 
    AS
    BEGIN
    DECLARE @Count INT;
    DECLARE @SeqNo VARCHAR(15)
    
    SELECT @Count = ISNULL(COUNT(SessionCode),0)
    FROM UserSessionLog
    WHERE SUBSTRING(SessionCode,0,9) =  CONVERT(VARCHAR(8), GETDATE(), 112)
    
    SET @SeqNo =  CONVERT(VARCHAR(8), GETDATE(), 112) +'-' + FORMAT(@Count+1,'D3');
    
    RETURN @SeqNo
    END
    

    generated codes are: '20170822-001' ,'20170822-002' ,'20170822-003'

提交回复
热议问题