完了,我脑海里一直是killygirl (开心女孩) 这几个字,什么同学寝室的,我不知道了!

解决方案 »

  1.   

    --先男,后女
    declare @i int,@j int
    select @i=0,@j=0update 学生表 set @i=case @j when 3 then @i+1 else @i end
    ,@j=case @j when 3 then 0 else @j+1 end
    ,寝室号=@i
    where 性别='男'select @j=0,@i=@i+1
    update 学生表 set @i=case @j when 3 then @i+1 else @i end
    ,@j=case @j when 3 then 0 else @j+1 end
    ,寝室号=@i
    where 性别='女'
      

  2.   

    譬如女生住在1号楼,男生住在2号楼
        寝室号分别是1xxx,2xxx
      

  3.   

    --测试--测试数据
    create table 表(学号 char(3),姓名 varchar(10),性别 char(2),寝室 char(4))
    insert 表 select '001','赵平','男',null
    union all select '002','王非','女',null
    union all select '003','李六','男',null
    union all select '004','李五','男',null
    union all select '005','张六','女',null
    union all select '006','张五','女',null
    union all select '007','张四','男',null
    union all select '008','李四','女',null
    union all select '009','张三','女',null
    go--更新参数定义
    declare @step int
    set @step=2 --这个2表示每两个人一个寝室--更新处理
    declare @i int,@j int--先处理男生的
    select @i=2001,@j=0update 表 set @i=case @j when @step then @i+1 else @i end
    ,@j=case @j when @step then 1 else @j+1 end
    ,寝室=@i
    where 性别='男'--再处理女生的
    select @i=1001,@j=0update 表 set @i=case @j when @step then @i+1 else @i end
    ,@j=case @j when @step then 1 else @j+1 end
    ,寝室=@i
    where 性别='女'
    go--显示处理结果
    select * from 表 order by 寝室,性别
    go--删除测试
    drop table 表/*--测试结果
    学号   姓名         性别   寝室   
    ---- ---------- ---- ---- 
    002  王非         女    1001
    005  张六         女    1001
    006  张五         女    1002
    008  李四         女    1002
    009  张三         女    1003
    001  赵平         男    2001
    003  李六         男    2001
    004  李五         男    2002
    007  张四         男    2002(所影响的行数为 9 行)
    --*/