Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:00:59+00:00 2026-05-30T18:00:59+00:00

is there’s a better way to get a daily sequence # from this sql

  • 0

is there’s a better way to get a daily sequence # from this sql proc

  DECLARE @id int

 UPDATE daily_identity 
 SET id = id + 1, @id = id + 1     
 WHERE day = CONVERT(VARCHAR(8), GETDATE(), 112) 
 IF @@ROWCOUNT = 0
     INSERT INTO daily_identity values ( CONVERT(VARCHAR(8), GETDATE(), 112), 1 )
     SELECT @id = 1
 END IF

 SELECT @id AS ‘id’
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:01:01+00:00Added an answer on May 30, 2026 at 6:01 pm

    Looks like an accident waiting to happen without a very restrictive transaction wrapped around it.

    EDIT: Emphasized that the Row_Number() calculation depends on numbering all of the rows.

    How about always doing an INSERT and using the result to generate a displayed daily Id? Do you see anything useful in this:

    -- Create some sample data.
    declare @Today as Date = GetDate()
    declare @Stuff as Table ( Id Int Identity, DateAdded Date )
    insert into @Stuff ( DateAdded ) values
        ( DateAdd( day, -3, @Today ) ),
        ( DateAdd( day, -2, @Today ) ), ( DateAdd( day, -2, @Today ) ), 
        ( DateAdd( day, -1, @Today ) ), ( DateAdd( day, -1, @Today ) ),
        ( @Today ), ( @Today )
    
    -- Display the test data.
    select Id, DateAdded,
        Id - ( select Min( Id ) from @Stuff where DateAdded = S.DateAdded ) as DailyId,
        Row_Number() over ( partition by DateAdded order by Id ) as AlternativeDailyId,
        Convert( VarChar(8), DateAdded, 112 ) + '-' +
            Right( '000000' + Cast( Id - ( select Min( Id ) from @Stuff where DateAdded = S.DateAdded ) as VarChar(10) ), 6 ) as DateId
        from @Stuff as S
    
    -- Add a row.
    declare @NewRows as Table ( Id Int, DateAdded Date )
    insert into @Stuff
        ( DateAdded )
        output inserted.Id, inserted.DateAdded into @NewRows
        values ( GetDate() )
    -- Display the new Id.
    --   Note the the   Row_Number()   calculation only works when all rows are counted.
    select N.Id, N.DateAdded,
        N.Id - ( select Min( Id ) from @Stuff where DateAdded = N.DateAdded ) as DailyId, -- NB: References @Stuff.     
        Row_Number() over ( partition by N.DateAdded order by N.Id ) as AlternativeDailyId,
        Convert( VarChar(8), N.DateAdded, 112 ) + '-' +
            Right( '000000' + Cast( Id - ( select Min( Id ) from @Stuff where DateAdded = N.DateAdded ) as VarChar(10) ), 6 ) as DateId
        from @NewRows as N
    select *
        from (
            select S.Id, S.DateAdded,
                S.Id - ( select Min( Id ) from @Stuff where DateAdded = S.DateAdded ) as DailyId, -- NB: References @Stuff.     
                Row_Number() over ( partition by S.DateAdded order by S.Id ) as AlternativeDailyId,
                Convert( VarChar(8), S.DateAdded, 112 ) + '-' +
                    Right( '000000' + Cast( Id - ( select Min( Id ) from @Stuff where DateAdded = S.DateAdded ) as VarChar(10) ), 6 ) as DateId
                from @Stuff as S
            ) as X
        where Id in ( select Id from @NewRows )
    

    Note that terrible things will happen if rows are deleted. The daily Ids will recalculate in thrilling ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is this SQL Statement SELECT t1.Name ,Count(t2.SubID) Totals -- I don't know how
There is previous little on the google on this subject other than people asking
There are a few ways to get class-like behavior in javascript, the most common
For some reason, after submitting a string like this Jack’s Spindle from a text
There is this strange situation I'm fighting on. I have 3 pages, les call
There are many tutorials that talk about deleting index.php from the url. But I
There must be a simple solution to this, but after 4 hours of browsing
There are things like f.call(...) f.apply(...) But then there's this (1, alert)('Zomg what is
There is a constant I'm using from the iOS SDK. I printed out the
There was some code like this: // Convenience to make things more legible in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.