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

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

    Can you use temporary table? This is a sample how to do this with a temp table.

    CREATE TABLE #MyTableVar (ID INT IDENTITY(1,1), SomeData NVARCHAR(300))
    
    insert #MyTableVar(SomeData) values ('test1'), ('test2')
    
    ---doesn't work
    DELETE FROM #MyTableVar 
    
    insert #MyTableVar(SomeData) values ('test3'), ('test4')
    select * from #MyTableVar 
    
    --resets the identity
    truncate table #MyTableVar
    insert #MyTableVar(SomeData) values ('test3'), ('test4')
    select * from #MyTableVar 
    

    Regards

    Piotr

提交回复
热议问题