积极答复者
TSQL问题

问题
答案
-
--不好意思,刚才写麻烦了 --> 测试数据: @T declare @T table (name varchar(1),taici int) insert into @T select 'a',1 union all select 'b',2 union all select 'c',3 union all select 'd',4 union all select 'e',5 update @T set taici= case when taici=5 then 2 when taici<2 then taici when taici>=2 then taici+1 when taici=2 then 5 end select * from @T order by taici /* name taici ---- ----------- a 1 e 2 b 3 c 4 d 5 */
- 已标记为答案 anhaisheng 2012年8月8日 15:06
全部回复
-
--> 测试数据: @T declare @T table (name varchar(1),taici int) insert into @T select 'a',1 union all select 'b',2 union all select 'c',3 union all select 'd',4 union all select 'e',5 --要改变的序号 declare @i int set @i=5 --改变后的序号 declare @j int set @j=2 --把5改成2,其他的顺延 ;with maco as ( select name, case when taici=@i then @j else taici end as c1, case when taici=@i then 2 else 1 end as c2 from @T ) ,t as ( select *,row_number() over (order by c1,c2 desc) as taici from maco ) --更新数据 update @T set taici=b.taici from @T a left join t b on a.name=b.name select * from @T order by taici /* name taici ---- ----------- a 1 e 2 b 3 c 4 d 5 */
-
--不好意思,刚才写麻烦了 --> 测试数据: @T declare @T table (name varchar(1),taici int) insert into @T select 'a',1 union all select 'b',2 union all select 'c',3 union all select 'd',4 union all select 'e',5 update @T set taici= case when taici=5 then 2 when taici<2 then taici when taici>=2 then taici+1 when taici=2 then 5 end select * from @T order by taici /* name taici ---- ----------- a 1 e 2 b 3 c 4 d 5 */
- 已标记为答案 anhaisheng 2012年8月8日 15:06