注册 登录
编程论坛 SQL Server论坛

有個問題各位來看看

jxyga111 发布于 2008-11-11 13:28, 863 次点击
現在要將table1 的資料導入到table2 中
以下為資料
table1
id name sex
001 a     0
002 b     0
003 c      1
004 d      0
005 e      1
以下為要的結果
table2
id name sex id2 name2 sex2
001 a      0   002   b         0
003  c      1   004   d         0
005   e     1

[[it] 本帖最后由 jxyga111 于 2008-11-11 18:33 编辑 [/it]]
4 回复
#2
小仙2008-11-11 19:30
能说下你每天都研究些啥吗?怎么都是这种怪要求
#3
jxyga1112008-11-11 20:36
回复 2# 的帖子
這個是有點怪,但是就是要這樣做出來,怎么做
#4
小仙2008-11-11 22:42
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
#5
happynight2008-11-11 22:48
为什么有 id name sex 还要有 id2 name2 sex2 不是很明白你怎样有这样的需求
1