• Why don't you let the database structure handle this with additional fields for date, type of entity (order, invoice, etc.), unique id(generated from SQL Server)?  The unique entity number can then be derived from the elements after the unique id is returned.  Your one-off-approach-for -this-client may prove difficult to maintain and likely to cause issues later on.  What if the table is corrupted, how do you reset the table value?