日期:2014-05-18  浏览次数:20441 次

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

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


declare @i int

set @i=0

update 表名 set 序号=@i,@i=@i+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