技术开发 频道

利用IDENTITY()函数重新为列生成序列

  【IT168技术文档】

  假设有个表table_tmp(id, name, info),其数据中id值随有一定顺序但并不是按序列排列,现在想让其id列按序列重新生成。可以使用以下办法:

  1、使用IDENTITY()函数为id生成从1开始的序列,并且把数据插入到新表my_table中。

select IDENTITY(int, 1, 1) as id, name, info into my_table from table_tmp

  2、删除掉原表数据

truncate table table_tmp or delete from table_tmp

  3、把新数据添加回原表,如果原表的id是identity类型,则注意使用

SET IDENTITY table_name ON select * into table_tmp from my_table

  4、删除掉中间表

drop table my_table
0
相关文章