Sunday, February 19, 2012

Data Type Range

Hi,

When I set a tinyint field as identity, it automatically sets the identity seed as 1. I wonder whether the increment is set to go around in loop. Say, I have the range of 256 numbers for a tinybit. So, an identifier should go from 1 to 255 and then to 0 and then back to 1. Is that right?

Not quite.

Once the limit is reached...crash

An identity column creates a constraint that does not allow duplicates and also an index to remember the last number generated.

Use int not tinyint and drop the field and recreate it if you need to.

Adamus

|||

As Adam alluded, an error will occur WHEN there is an attempt to add a 256th row to the table (since the tinyint ranges from 0 to 255, and the IDENTITY column started at 1).

This code will help demonstrate the consequences:

Code Snippet

SET NOCOUNT ON

DECLARE @.MyTable table
( RowId tinyint IDENTITY,
Name varchar(20)
)

DECLARE @.Counter int

SET @.Counter = 1

WHILE @.Counter <= 260

BEGIN
PRINT @.Counter
INSERT INTO @.MyTable VALUES ( @.Counter )
SET @.Counter = ( @.Counter + 1 )
END

|||Thanks

No comments:

Post a Comment