Adding an identity to an existing column

后端 未结 19 2074
温柔的废话
温柔的废话 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条回答
  •  失恋的感觉
    2020-11-21 13:44

    Consider to use SEQUENCE instead of IDENTITY.

    IN sql server 2014 (I don't know about lower versions) you can do this simply, using sequence.

    CREATE SEQUENCE  sequence_name START WITH here_higher_number_than_max_existed_value_in_column INCREMENT BY 1;
    
    ALTER TABLE table_name ADD CONSTRAINT constraint_name DEFAULT NEXT VALUE FOR sequence_name FOR column_name
    

    From here: Sequence as default value for a column

提交回复
热议问题