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 7691833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:42:49+00:00 2026-05-31T20:42:49+00:00

I am saving documents to database, each document has to have an id with

  • 0

I am saving documents to database, each document has to have an id with the format YYYY-00000:

  • the first 4 characters are the current year
  • the second five characters are numbers. They start with 1 each year and then increment.

For example I could have these documents in my database: 2011-00001, 2011-00002, 2011-00003, 2012-00001, 2012-00002, …

I am thinking something like this:

  • add two columns to table Documents (Year and Number)
  • Year is computed column, something like year(getdate())
  • Number is computed column, which gets value from a function GetNextNumberForCurrentYear
  • GetNextNumberForCurrentYear returns next number for the current year (for example select max(Number) + 1 from Documents where Year = year(getdate()), and some isnull checking)

But i fear, that two users could want to save the document at the same time and that they would receive the same Number. Is this possible? Any better ideas?

It is a ASP.NET C# web application, .NET 4.0, MSSQL 2005, I have the control over all the parts of the application.

PS: after insert I would like to return the Id of the new document to the user, so I would probably have to do something like: select Id from Documents where SomeId = scope_identity(), so I guess there should be an identity column somewhere…?

Edit (final solution): I get the next number from stored procedure, build the Id of the document (in format YYYY-00001) in .NET, save the whole document to the database (using TransactionScope for whole process) and then return the Id to the user.

create table DocumentNumbers ([Year] int not null, Number int not null default 1)
insert into DocumentNumbers ([Year], Number)
select 2012, 1 -- and more...

create procedure GetNextDocumentNumber
    @year int
as
begin
    declare @myResult table (nextNumber int) 

    update DocumentNumbers 
    set Number = isnull(Number, 0) + 1 
    output inserted.Number into @myResult 
    where [Year] = @year

    select top 1 nextNumber from @myResult
end
  • 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-31T20:42:50+00:00Added an answer on May 31, 2026 at 8:42 pm

    You could create a table NumberSeries, which contains a column Year and a column CurrentNo and a function that returns the next number from it like the following:

    DECLARE @myResult TABLE (nextNumber INT)
    
    UPDATE NumberSeries
    OUTPUT INSERTED.NextNo INTO @myResult
    SET
        CurrentNo = ISNULL(CurrentNo, 0) + 1
    WHERE
        Year = Year(GetDate())
    
    DECLARE @result INT
    @result = (SELECT TOP 1 nextNumber FROM @myResult)
    
    RETURN @result
    

    This updates the NumberSeries table atomically and inserts the new value into the @myResult table variable. After that, it returns the first (and only) value from the @myResult table variable.

    Everything else, like SCOPE_IDENTITY() and such may cause errors when using triggers or in other cases – the solution using the OUTPUT clause is safe.

    EDIT
    As for returning the ID of the inserted document: this is basically the same thing.

    DECLARE @myDocId TABLE (yr int, no int)
    
    INSERT INTO Documents
    OUTPUT INSERTED.Year , INSERTED.YearID INTO @myDocID
    ...
    
    SELECT TOP 1
        CAST(yr AS NVARCHAR) + 
        '_' + 
        RIGHT(REPLICATE('0', 5) + CAST(no AS NVARCHAR), 5) AS NewID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm saving whole HTML documents in the database with ActiveRecord. I can see in
I have an iPhone application that uses a sqlite3 database for saving data and
Hi I have a problem saving 2 tables of my database at the same
Saving a Word 2003 document to XML and then back results in a reduced
I'm having trouble saving a new row in the DataGridView back to the database.
I have a web application that uses a relational database (MySQL). We're adding a
I've got a richtextbox, that I plan on saving to a database, which can
This is a bit of a tricky one. I have Document entities that are
My page conisits of x amount of forms. One for each database entry. The
In a App. i am saving the images from server in Document Directory. 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.