I need help insering an id into the database in ASP.net mvc (C#). Here theid is the primary key and it should be in the format 24-073110-XX, where XX represents a numeric value which should be incremented by 1.
How should I insert the id in this format?
As Rob said – don’t store the whole big identifier in your table – just store the part that changes – the consecutive number.
If you really need that whole identifier in your table, e.g. for displaying it, you could use a computed column:
This way, your INT IDENTITY will be used as an INT and always contains the numerical value, and it will be automatically incremented by SQL Server.
Your
DisplayIDfield will then contain values like:Since it’s a persisted field, it’s now part of your table, and you can query on it, and even put an index on it to make queries faster:
Update:
I would definitely not use
DisplayIDas your primary key – that’s what theID IDENTITYcolumn is great forto create an index on
DisplayIDis no different than creating an index on any other column in your table, really: