Is there anyway to reset the identity of a Table Variable?

前端 未结 12 2033
无人及你
无人及你 2020-12-17 15:33

Say I have a table variable:

DECLARE @MyTableVar TABLE (ID INT IDENTITY(1,1), SomeData NVARCHAR(300))

After I have inserted 250 rows, I nee

12条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 15:51

    Instead of re-seeding the IDENTITY, why not just delete from the @table variable, then use ROW_NUMBER() against the input? e.g. instead of the lazy

    SELECT * FROM @MyTableVar;
    

    ...use...

    SELECT ID = ROW_NUMBER() OVER (ORDER BY ID), SomeData FROM @MyTableVar;
    

    Now you don't need to care what the seed is, whether it starts at 1, whether there are any gaps, etc.

提交回复
热议问题