Arithmetic overflow error converting IDENTITY to data type tinyint

妖精的绣舞 提交于 2019-12-07 01:52:39

问题


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?

回答1:


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]



回答2:


... 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.



来源:https://stackoverflow.com/questions/27040938/arithmetic-overflow-error-converting-identity-to-data-type-tinyint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!