>> "SET IDENTITY_INSERT ON" would be needed for prior year, with logic similar to above, but hopefully that would be relatively rare. <<
In fact, in most systems, especially dealing with invoices, it should never be allowed -- once the current year is past, no new invoices with the prior yr on them should be created.
Mark,Where are you updating the stored value? I have used this a few times and have not seen any impact on scalability but it may not have been under a big enough load. The statement as I posted it gets in and out of the table very quickly so I am not sure what kind of concurrency problems you might be referring to. Please give us a little more detail before I use this again!
Scott,I like your idea as well. It wouldn't be hard to create a job that ran at the beginning of every year to reset the seed and increment of the identity column. The only downside (very minor imo) would be that you can't put any non-numeric characters in the field itself.
The Idea of a Sequence table is the right idea in this case.
"Central location for keys is a concurrency nightmare" if you are not using the appropriate locking mechanisms.
As you can see I have experience with this structure and have been successfuly able to implemet such construct.
It is not my preffered way but it is very portable and very flexible
Noel, you're confusing me. "A sequence table is the right idea", but "It is not my preferred way"? So there's another way you prefer that's better than the ones posted, but you're not going to share it with us?
I agree that the sequence table is a very workable solution and would not worry about scaling if it is done right. My biggest concern would be guaranteeing that all inserts are done by a stored procedure that correctly accesses the sequence table.
You could argue that the identity field approach should scale better because you only need one action to insert the record, while the sequence table requires an extra action for the update. This is probably just a theoretical argument, because I can't imagine a real situation where a sequence table approach would break down if all of Noel's suggestions were applied.
On the other hand if you've got such a volume of invoices that you're worried about scaling, you should be able to afford enough hardware to deal with it. I'd be happy to come help set up your new 32-processor servers for the reasonable sum of twice my current salary.
One of my concerns with a sequence table, aside from the additional I/O and other overhead, is that you cannot then easily insert multiple invoices in one statement.
If, for example, thru some EDI (yes, it is still used) or other process you have a batch of invoices to enter, you must single-step thru them with a sequence table, but not with an identity column.
As for a non-numeric, that's true, it could not be in the column itself -- then again, it shouldn't be anyway imo. It should be a separate column and concatenated into the computed column that is displayed to the user.
I'm not sure how the function will work with multiple rows, I get a duplicate key error when I try it.
Maybe if it was implemented as a CLR function and flagged as non-deterministic, but I don't think it's worth the trouble.