**//*

奇數行將前一半的按增序排,偶數行將後一般按增序排。以下為表數據:

column1 column2 column3

-----------------------

11 12 13

13 14 15

21 22 23

22 23 24

12 13 14

25 26 27

排完以後應為:

column1 column2 column3

-----------------------

11 12 13

21 22 23

12 13 14

22 23 24

13 14 15

25 26 27

以下為實現方式:

*/


create table t0(column1 int, column2 int, column3 int)

insert t0

select 11, 12, 13

union all select 13, 14, 15

union all select 21, 22, 23

union all select 22, 23, 24

union all select 12, 13, 14

union all select 25, 26, 27


create table #t(column1 int, column2 int, column3 int)



DECLARE @c1 int,@c2 int,@c3 int,@n int,@a int,@b int
select @n=count(*) from t0

if (@n%2 =1) set @a=@n/2+1 else set @a=@n/2

set @b=@n-@a


exec('declare crs1 cursor for select top '+@a +' * from t0 order by column1')

exec('declare crs2 cursor for select * from (select top '+@b +' * from t0 order by column1


desc) as b order by column1 asc')

open crs1

open crs2



while @n>0

begin

print @n

if @n%2=0

begin

FETCH NEXT FROM crs1 INTO @c1 ,@c2 ,@c3

insert into #t values(@c1 ,@c2 ,@c3)

end

else

begin

FETCH NEXT FROM crs2 INTO @c1 ,@c2 ,@c3

insert into #t values(@c1 ,@c2 ,@c3)

end


set @n=@n-1

end


select * from #t


DEALLOCATE crs1

DEALLOCATE crs2

drop table #t


drop table t0