select identity(int,1,1) as seq_no,[id] as a, 2 as z into #t from sysobjects
create table #table(
seq_no int identity(1,1),
a int,
b int,
z int
)
--select * from #t where seq_no % 2=1
insert into #table select a.a,b.a as b,a.z from (select * from #t where seq_no % 2=1) a,(select * from #t where seq_no % 2=0) b where a.seq_no+1=b.seq_no
select * from #t
select * from #table
drop table #t
drop table #table
你这个表应该有主键我先当它为ID,不知道你的a,b,z是什么型的,先设为INT的
create table t (a int, b int, z int, id int)
Declare @Min int, @Max int, @id int
Select @min = min(id), @max = Max(id) From 表
While @min <= @max
begin
if EXISTS (Select * From 表 where id = @min)
begin
Insert t (id, a, z) Select id, a, z From 表 where id = @min
Select @id = min(id) From 表 Where @id > id
updqte t set b = s.a From 表 s Where id = @min
end
Set @min = @min + 1
end