Adding an identity to an existing column

后端 未结 19 2096
温柔的废话
温柔的废话 2020-11-21 13:16

I need to change the primary key of a table to an identity column, and there\'s already a number of rows in table.

I\'ve got a script to clean up the IDs to ensure

19条回答
  •  猫巷女王i
    2020-11-21 13:54

    I don't believe you can alter an existing column to be an identity column using tsql. However, you can do it through the Enterprise Manager design view.

    Alternatively you could create a new row as the identity column, drop the old column, then rename your new column.

    ALTER TABLE FooTable
    ADD BarColumn INT IDENTITY(1, 1)
                   NOT NULL
                   PRIMARY KEY CLUSTERED
    

提交回复
热议问题