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

前端 未结 12 2022
无人及你
无人及你 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:34

    Since you are re-using your table, if I got it right, how about you do not initialize your counters to 1 and instead use this as an example?

    DECLARE @ctr INT
    IF @ctr IS NULL or @ctr <= 0 --this part is to control @ctr value on loops
       SET @ctr = 1
    ELSE
       SELECT @ctr = MIN(id) FROM @tbl
    

    This way, you are not restarting your loop to 1 nor is there a need for you to truncate the table.

提交回复
热议问题