Add a column to existing table and uniquely number them on MS SQL Server

前端 未结 8 1291
暖寄归人
暖寄归人 2020-12-04 08:16

I want to add a column to an existing legacy database and write a procedure by which I can assign each record a different value. Something like ad

8条回答
  •  没有蜡笔的小新
    2020-12-04 08:37

    It would help if you posted what SQL database you're using. For MySQL you probably want auto_increment:

    ALTER TABLE tableName ADD id MEDIUMINT NOT NULL AUTO_INCREMENT KEY

    Not sure if this applies the values retroactively though. If it doesn't you should just be able to iterate over your values with a stored procedure or in a simple program (as long as no one else is writing to the database) and set use the LAST_INSERT_ID() function to generate the id value.

提交回复
热议问题