SQL中如何更新下表....

来源:百度知道 编辑:UC知道 时间:2024/06/30 03:02:53
表a
a1 b2 c3
005 2 a
005 3 b
005 3 a
007 4 a
009 3 b
009 4 b
010 3 a
011 3 b

现在我想更新表a中的a1字段为
a1 b2 c3
001 2 a
001 3 b
001 3 a
002 4 a
003 3 b
003 4 b
004 3 a
005 3 b

a字段中为数字,无规律,由小到大且有重复
现我想更新为 由小到大 ,依次递增1,但原表中重复纪录必须重复,请教高手~~
a1为某日的操作流水号~~~操作类型有几样,所以操作流水有重复~
而现在我需要根据某个条件对其进行排序。。。。

例如
a1
200704290001
200704290001
200704290001
200704290003
200704290003
200704290007
200704290012
200704290012
200704290012

要更新成
200704290001
200704290001
200704290001
200704290002
200704290002
200704290003
200704290004
200704290004
200704290004

并且上面的a1是我通过查询select a1 form a where id = 'xxx' 得出的。。。。

什么数据库呀
用语句搞不定了
得用存储过程了
用HI给我留言吧
我试试

最后结果,换了表和字段就行了:
update tablename set a1=left(a1,8)+
RIGHT(CAST(CAST(
(select count(distinct a1) from tablename a where a.a1<=tablename.a1)
AS INT)+100000 AS VARCHAR),4)
where xxxx order by a1

declare @i int,@j int
select @i=count (1) from (select distinct a1 from a)t --得到全部不重复的列,好方便给ID加1
set @j=1 --while条件,并且是后来id值
declare @str varchar(2000)
while @j<=@i
begin
set @str='update a set a1='
if len(@j)=1 --如果id长度为1 则插入'00'+id
begin
set @str='update a set a1=''00'+cast(@j as varchar(10))+''' where a1=(select top 1 a1 from
(select distinct top '+cast(@j as varchar(10))+' a1 from a order by a1 asc) t order by a1 desc)'
end
else if len(@j)=2 --为2 则插入'0'+id
set @str=@str+'''0'+cast(@j as varchar(10))+''' where a1