create table table1
(
id char(3) not null primary key,
name varchar(10),
sex bit
)
go
create table table2(id char(3), name varchar(10), sex bit, id2 char(3), name2 varchar(10), sex2 bit)
insert into table1
values('001','a',0)
insert into table1
values('002','b',0)
insert into table1
values('003','c',1)
insert into table1
values('004','d',0)
insert into table1
values('005','e',1)
declare mycur cursor
for select * from table1
open mycur
declare @id char(3),
@name varchar(10),
@sex bit,
@id2 char(3),
@name2 varchar(10),
@sex2 bit,
@i int
set @i =1
fetch next from mycur
into @id,@name,@sex
while @@fetch_status=0
begin
if @i%2 <>0
begin
if @i <> 5
begin
fetch next from mycur
into @id2,@name2,@sex2
insert into table2
values(@id,@name,@sex,@id2,@name2,@sex2)
end
else
begin
insert into table2(id,name,sex)
values(@id,@name,@sex)
end
set @i = @i+1
end
else
begin
fetch next from mycur
into @id,@name,@sex
set @i = @i+1
end
end
close mycur
deallocate mycur
小仙写的