Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 TRUNCATING and Enabling INDENTITY_INSERT

Author  Topic 

ronstone
Starting Member

32 Posts

Posted - 2004-02-27 : 10:00:26
If someone knows the behavior, it would save me time in testing this out...

If I truncate a table that has an identity column, then insert into the table (from an identical table with less rows) with identity_insert ON, will SQL be smart enough to know which identity to correctly use for subsequent row inserts?

I need to copy the identity column because this column is used as a foreign key on another table.

Thanks!
Ron

ronstone
Starting Member

32 Posts

Posted - 2004-02-27 : 10:14:31
Update!

First I truncated the table.

Ran DBCC CHECKIDENT (tablename, noreseed). This reports the current identity. It was reset to 1.

Imported the table with the identity_insert ON.

Ran DBCC CHECKIDENT (tablename, noreseed) again. It showed the current max(identitycolumnvalue). So, it looks like on the insert, it reseeded to the maximum value.

Go to Top of Page

ronstone
Starting Member

32 Posts

Posted - 2004-02-27 : 10:28:27
Sorry, another update.

When you DELETE FROM Table, the Identity does NOT get reseeded. TRUNCATE does reseed to 1.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-02-27 : 10:54:05
Truncate will set the seed to the original value.
When a row is inserted with identity_insert - if the value is greater than the current seed (or less if the value is set to decrease) then the new value is used if not the seed is unchanged.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -