Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic overflow error converting IDENTITY to data type tinyint

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.

  1. How to force database engine to check what indexes are free?
  2. Will this happen too if I change tinyint to int and reach limit of int number?
like image 470
Ondřej Avatar asked Nov 20 '14 13:11

Ondřej


2 Answers

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.

DELETE FROM [TestTable]

DBCC CHECKIDENT ('[TestTable]', RESEED, 0)
GO

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.

Truncate Table [TestTable]
like image 89
HaveNoDisplayName Avatar answered Sep 28 '22 03:09

HaveNoDisplayName


... even after I delete all rows in that table

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.

Will this happen too if I change tinyint to int and reach limit of int number?

Yes, it will happen for any type that can overflow.

How to force database engine to check what indexes are free?

This would kind of defeat the purpose of an identity as you would be reusing previous values.

like image 24
Alex K. Avatar answered Sep 28 '22 03:09

Alex K.