I am designing table in SQL Server 2008 R2 SP2 Express database. I created table with nvarchar
column for data and tinyint
column for auto-incrementing identity assuming there will be no more than few rows (that's why I choose 0-255 tinyint
). When I add more than 255 rows, this error keeps occurring permanently even after I delete all rows in that table and try to add one new row. I am doing this using SQL Server Management Studio Express.
- How to force database engine to check what indexes are free?
- Will this happen too if I change
tinyint
toint
and reach limit ofint
number?
The point of an identity is that it will increment the last assigned value irrespective of deletes, so as soon as you exceed the capacity of the identity type, that error is permanent.
Yes, it will happen for any type that can overflow.
This would kind of defeat the purpose of an identity as you would be reusing previous values.
Once you add 255 rows, then want to delete all rows from table, But the counter of increment column is already set to 255, even after delete all rows.
To solve this, you need to reset counter of increment column after deleting all rows. Please use below query when deleting all rows, so that counter will reset to reset to 0. After execute this query, you can add rows in table with increment column values as 1 or what you set at table design time.
You can also use truncate table command, if you want to delete all rows from table. Truncate Command also reset increment column to initial value.