How can I reseed an identity column in a T-SQL table variable?

后端 未结 5 1427
梦毁少年i
梦毁少年i 2020-12-08 09:34

I have a T-SQL table variable (not a table) which has an auto incrementing identity column. I want to clear all data from this variable and reset the identi

5条回答
  •  执念已碎
    2020-12-08 10:25

        declare @tb table (recid int,lineof int identity(1,1))
    
        insert into @tb(recid)
        select recid from tabledata 
    
        delete from @tb where lineof>(select min(lineof) from @tb)+@maxlimit
    

    I did this when I wanted to use a TOP and a variable when using SQL 2000. Basically, you add in the records and then look at the minimum one. I had the same problem and noticed this thread. Deleting the table doesn't reset the seed although I imagine using GO should drop the table and variable to reset the seed.

    @maxlimit in the query above was to get the top 900 of the query and since the table variable would have a different starting identity key, this would solve that issue.

    Any subsequent query can subtract that derived procedure to make it insert as "1", etc.

提交回复
热议问题