Programmatically set identity seed in a table variable

后端 未结 3 806
你的背包
你的背包 2021-01-18 06:30

I need to create a table variable with an identity seed that starts with the max value of a field in another table?

I\'ve tried this:

DECLARE @Identi         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-18 07:33

    You can check the current value of an IDENTITY column by using:

    DBCC CHECKIDENT (#HH)
    

    and you can also change that later on using:

    DBCC CHECKIDENT (#HH, RESEED, 42)
    

    and that also works with a variable for the new value:

    DBCC CHECKIDENT (#HH, RESEED, @IdentitySeed)
    

    It works for local and global temporary tables (i.e. CREATE TABLE #HH (...) or CREATE TABLE ##HH (....) - but it doesn't seem to work with table variables :-(

    Sorry, it seems you can't do this with table variables.....

提交回复
热议问题