Can we roll back after we used DBCC CHECKIDENT to restart the identifier column?

Currently, on some operations, I have to delete the old data and insert a new one. But I noticed that despite the deletion of the data, the identity column was not reset and continued from its last maximum value. Therefore, I used DBCC CheckIdent to achieve the same as all this happening within the framework of trasaction. Can I return a transaction back to intital state? Is there a problem with DBCC CHECKIDENT? Kindly leadership ...

+3
source share
1 answer

The test code below shows that the DBCC action can be rolled back:

create table #t
(id int identity, val1 int)
go

insert #t (val1)
values (1),(2),(3)

select MAX(id) AS before from #t

begin tran 

    delete #t

    dbcc checkident (#t, reseed,0)

    select MAX(id) AS inside_tran from #t   

rollback

select MAX(id) as after_rollback from #t
dbcc checkident (#t, noreseed)
+6
source

Source: https://habr.com/ru/post/1768016/


All Articles