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

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

    0 讨论(0)
  • 2020-12-17 15:49

    unfortunately there is no function to reseed identity column in table variable, I know this question is very old, but in case other people encountered the same problem, I would like to share my method to solve this problem.

    /* declare another table variable with same structure and perform select insert*/
    
    DECLARE @MyTableVar1 TABLE (ID INT IDENTITY(1,1), SomeData NVARCHAR(300))
    insert into @MyTableVar1
    select someData from @MyTableVar
    

    However, If you want to perform dynamic reseeding inside a loop, I would suggest using a table object

    0 讨论(0)
  • 2020-12-17 15:51

    Instead relying on an Identity, why not use the new ranking functions such as Row_Number

    Insert @MyTableVar( Id, Value )
    Select Row_Number() Over ( Order By Value )
        , Value
    From SomeOtherTable
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-17 15:52

    you should truncate your table instead of deleting all rows from it.

    but note that truncate will not work for some tables, (listed from MSDN):

    You cannot use TRUNCATE TABLE on tables that:
    
    Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
    
    Participate in an indexed view.
    
    Are published by using transactional replication or merge replication.
    

    and added myself:

    You cannot truncate a table variable.
    

    syntac of truncate is:

    TRUNCATE TABLE 
        [ { database_name .[ schema_name ] . | schema_name . } ]
        table_name
    [ ; ]
    

    EDIT: I didn't notice that you are questioning about table variables.

    as far as I know there is no way to reset an identity column in a table variable. you can use a temp table instead.

    0 讨论(0)
  • 2020-12-17 15:53

    I tried it on net but i am not able to get any solution on reset identity for table variable.

    If you are able to use temp table #MyTableVar instead of table @MyTableVar variable then it is possible to reset identity value

       DBCC CHECKIDENT('TableName', RESEED, NewValue)
    
       DBCC CHECKIDENT(#MyTableVar, RESEED, 0)
    

    Newvalue must be one less than the newIdentiyValue

       NewValue= NewIdentity-1;
    

    If you still want to learn more you can refer my blog http://tryconcepts.blogspot.in/2012/08/reset-identity-column-to-new-id.html

    0 讨论(0)
提交回复
热议问题