3

有人禁用了 SQL DB 中表的标识列。即使表中已有数据,是否可以重新启用该列的功能?并保持现有的身份价值?

我知道我可以将数据复制到另一个表并在设置 Identity_Insert 后重新插入。

4

2 回答 2

6

您无法打开现有列IDENTITY,这在 SQL Server 中是不可能的(至少到 2012 版)。

您需要做的正是您所描述的:

  • 以您想要的结构创建新表,其中包含IDENTITY
  • 将现有表中的数据复制到新表中,使用SET IDENTITY_INSERT ON
  • 放下旧桌子
  • 将新表重命名为旧表名

您可以在 SQL Server Mgmt Studio 的可视表设计器中“重新启用”标识规范,但这实际上只为您在后台执行上述步骤。

于 2013-07-19T16:53:44.623 回答
3

正如 *marc_s* 所说,您可以使用

我不知道是否有任何其他方法可以做到这一点,但你可以使用

    CREATE TABLE tblNewTable
    (
         //Put the columns and datatypes of the former table
    )

    INSERT INTO tblNewTable
    AS
    SELECT * FROM oldTable

然后使用删除表

    DROP TABLE oldTable

然后重新创建新表并添加标识列,然后使用

   INSERT INTO tblNewRecreatedTable (//Columns of the new created table except the column with the identity
   AS
   SELECT //Columns of the table you copied the data to except the Columned that you  defined identity

我希望它有帮助

于 2013-07-19T17:00:02.813 回答