sql更新序号问题,急急急急!!

来源:百度知道 编辑:UC知道 时间:2024/06/28 04:06:45
我原来有一个表
id data
f01 a1
f02 a2
f03 a3
f04 a4
```` ```
应为数据行的删除和添加 有了缺项 和 多项成了
id data
f01 a1
f03 a3
f04 a4
```` ```
现在考虑改成
id data
h001 a1
h002 a3
h003 a4
``` ```
我是新手,谁能给我个简单的实例 谢谢了

还有一个问题,表的1列是电话号码,如果是手机就不管,如果是座机或小灵通就在前面加区号,要怎么做??

为什么我前面的提问会没有了呢??

//用循环,但是效率不高,数据量大的话不建议,我的方法比较笨:)

declare @maxnum int
declare @idx int
declare @realidx int
set @idx=1
set @realidx=1
select top 1 @maxnum=cast(substring(data,2,5) as int) from table1 order by cast(substring(data,2,5) as int) desc
while @idx<=@maxnum
begin
select * from table1 with(nolock) where data='a'+cast(@idx as varchar(4))
if @@rowcount>0
begin
update tbltest set [id]='h'+cast(@realidx as varchar(4)) where data='a'+cast(@idx as varchar(4))
set @realidx=@realidx+1
end
set @idx=@idx+1
end

我刚刚发明了最好的SQL排序号的方法,给你先用一用,无需游标,只要能保证ID没有重复,用这个方法即快又方便:
select newid=(select count(1),oldid=id into #临时表 from 表名 a where a.id>=表名.id),id from 表名
update 表名 set id='h'+right(convert(varchar,newid+10000),3) from 表名,#临时表 where oldid=newid