declare @name varchar(20)
select @name=top 1 name from table order by id desc
update table set name=a.name from table a where id=a.id+1
update table set name=@name where id=1
select @name=top 1 name from table order by id desc
update table set name=a.name from table a where id=a.id+1
update table set name=@name where id=1
declare @name2 varchar(20)
declare @mid int
select identity(int, 2, 1) as aaid,* into @Tmp from tableselect @mid = (select max(id) from #Tmp)
select @name1 = (select name from #Tmp where aaid = 1)
select @name2 = (select name from #Tmp where aaid = @mid)update table set name = @name2 where id = 1
update table set name = @name1 where id = @middrop table table
select * into table from @Tmp
go
id name
1 张三
2 李四
3 吴奖德
4 冯仁坤
需要做循环一次后如下:
id name
1 冯仁坤
2 张三
3 李四
4 吴奖德
我需要的是把name字段的值循环,最上的往下放,最下的放在上面。
To:yelook(雨枫) 、nielisheng(阿土)兄,你们的代码没有循环啊。
asdeclare @max int
select @max= max[id] from Utableselect (case when [id]< @max then [id]+1 when [id] = @max then 1 else [id]-1 end) as tid,[name]
into #T
from Utable order by tidupdate Utable
set [name] = #T.[name]
from #T
where [id] = #T.tid
declare @max int
declare @name char(10)
select @max = max(id) from tablename
set @id = @max
select @name = name from tablen where id = @id
while @id > 1 then
begin
update tablename set A.name = B.name from tablename as A , tablename as B where A.id = @id and B.id = @id -1
set @id = @id - 1
end
update tablename set name = @name where id = 1
我想好像没有问题啊。但是提示却......
declare @max_id int
select @min_id = min(id), @max_id = max(id) from tableupdate table set id = (id + 1) mod (@max_id + 1) + @min_id
go