首页 > 数字化转型 > ERP > 利用IDENTITY()函数重新为列生成序列
假设有个表table_tmp(id, name, info),其数据中id值随有一定顺序但并不是按序列排列,现在想让其id列按序列重新生成。可以使用以下办法:
[@more@]1、select IDENTITY(int, 1, 1) as id, name, info into my_table from table_tmp
使用IDENTITY()函数为id生成从1开始的序列,并且把数据插入到新表my_table中。
2、truncate table table_tmp or delete from table_tmp
删除掉原表数据
3、select * into table_tmp from my_table
把新数据添加回原表,如果原表的id是identity类型,则注意使用SET IDENTITY table_name ON
4、drop table my_table
删除掉中间表
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/66009/viewspace-799246/ ,如需转载,请注明出处,否则将追究法律责任。