gpt4 book ai didi

sql-server - DBCC CHECKIDENT(myTable, RESEED,1) 从 2 重新播种

转载 作者:行者123 更新时间:2023-12-01 13:31:50 26 4
gpt4 key购买 nike

在 SQL Server 2012 中,以下查询为标识列 myTable_id 设定种子从 2 而不是 1。为什么? myTable_id也是PK。

DELETE FROM myTable;
GO
SELECT * FROM myTable --0 rows are returned as expected
GO
DBCC CHECKIDENT(myTable, RESEED,1)
GO
INSERT INTO myTable(col1,col2,col3) SELECT FROM AnotherTable(col1,col2,col3)
GO
SELECT * FROM myTable --1005 rows are returned as expected, but identity value starts from 2
GO

备注 :
  • 插入的数据是对的,唯一的问题是新插入的数据是从2开始而不是1开始的。
  • 在上面的 sql 代码中,如果我使用 DBCC CHECKIDENT(myTable, RESEED,0)标识列正确地从 1 开始。
  • 以下是 myTable_id 的 SSMS 快照栏目:

  • enter image description here

    最佳答案

    来自 docs :

    The seed value is the value inserted into an identity column for the very first row loaded into the table. All subsequent rows contain the current identity value plus the increment value where current identity value is the last identity value generated for the table or view.



    因此,如果您从 10 开始播种,则下一个要插入的值将是 11。

    关于sql-server - DBCC CHECKIDENT(myTable, RESEED,1) 从 2 重新播种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45618332/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com