当前位置: 代码迷 >> Sql Server >> 用一条SQL语句怎么解决一张表中ID编号的有关问题
  详细解决方案

用一条SQL语句怎么解决一张表中ID编号的有关问题

热度:47   发布时间:2016-04-27 19:53:22.0
用一条SQL语句如何解决一张表中ID编号的问题?
我有一张表,字段ID的内容是从别的表中提过来的,所以ID号是乱的。我想把ID按照它在该表的摆放顺序对ID的值用顺序号进行填充,如该记录在表中的顺序号是1则ID编号就填1,以此类推。但不能循环一条一条的对ID编号进行填充,那样循环一遍很费时间。不知道有没有该SQL语句?

------解决方案--------------------
--try


declare @i int

set @i=0

update 表名 set [email protected],@[email protected]+1
------解决方案--------------------
update a set id=(select count(1) from [table] where id <=a.id) from [Table] a
------解决方案--------------------
ID不重复吧?

select id1=identity(int 1,1) , id into temp from tb

update tb
set id = id1
from tb,temp
where tb.id = temp.id

drop table temp
  相关解决方案