I had a similar problem with my installation of VS 2012 (Premium). I installed VS 2012, at the custom installation options screen I chose the option for SSDT, but when actually using it, it was really absent (same error as yours).
My idea is that the default installer only adds the template for the project, but not SSDT itself. I also had to install SSDT separately (in this case only for VS 2012) and it worked (SSDT for VS 2010 is necessary only if you want to open SQL projects in VS 2010, but I guess you know that already).
Randomly, one of these tables' identity values will fall behind, stopping any inserts from happening and we have no idea why.
Inserts are probably stopping because of an attempt to reuse an already existing unique value in the PRIMARY KEY, thus triggering the error like:
Msg 2627, Level 14, State 1, Line 27
Violation of PRIMARY KEY constraint 'PK__ID__1234'. Cannot insert duplicate key in object 'dbo.MyTable'. The duplicate key value is (8).
The statement has been terminated.
Note that if your IDENTITY
column does not have a UNIQUE
index or constraint, it is possible to reseed repeatedly and have many identical ID
values. You do not want to do that, of course.
I have not personally found an error that, in itself, would reseed the IDENTITY
value. Of course, it is possible to reset the SEED
to a range where there will soon be a conflict by running a reseed that is lower than the current seed:
DBCC CHECKIDENT( MyTable,RESEED, 7) WITH NO_INFOMSGS
It could be that code somewhere in one of the processes actually does a RESEED
on the table under some unusual circumstances.
(For example, this could be from a merge of two data sets, where the code reads the high value from one data set and after the import RESEEDs to the lower of the two high values that were merged.)
You should also read Martin Smith's post at: https://stackoverflow.com/questions/14146148/identity-increment-is-jumping-in-sql-server-database
Best Answer
One way would be to add an
ID
column in your users table, with a default value based on a sequence object, then add a computed column to create the key using a date conversion to string.To create the sequence, something like:
I wouldn't limit yourself to a value of 99 if you are not 100% sure you are going to stay under that number of users per day.
You want a fresh cycle every day, right? So you can force it with an evening job/script to ensure it is back at 1 the next day.
So just to illustrate the point, say I'm creating a new table,
UserDetails
, and want to include this logic, I might do something like:My table content would be:
You asked about indexing, so, ignoring the fact that the above example would be non-deterministic and therefore ineligible to be used a primary key, if you want to index the computed column, you'll need to PERSIST the column in the database. This stores the values to disk rather than as a calculated virtual value.
Note that there are various restrictions with what you can do with computed columns so I would implore you to read the following information to get a proper understanding.
MSDN Link
Here's a relevant snippet from that article, which also relates to the above example: