请问为什么while @d<@a没有循环?declare 
@a int ,@b int,@c int ,@d int ,@e int ,@f varchar(50) ,@g varchar(50)
create table #person
([idd] int IDENTITY (1, 1),
username varchar(50))
create table #windows
([idd] int IDENTITY (1, 1),
username varchar(50))
insert into #person (username )
select c_username  from user_table
insert into #windows (username )
select  username from windows
select @a=(select max(idd) from #person),@b=(select min(idd) from #person)
select @c=(select max(idd) from #windows),@d=(select min(idd) from #windows)
while @b<=@a
     begin
         select @f=(select   username  from #person   where idd =@b)   
         while @d<=@c
             begin
             select @g=(select username from #windows where idd=@d)
             select @e=(select count(*) from rights_table where c_username=@f and c_description=@g)
               if @e=0
                    begin
                        insert into rights_table(c_username,c_description)values(@f,@g)
                      end
                 set @d=@d+1
             end
           set   @b=@b+1
select @a,@b,@c,@d,@e,@f,@G
 end 
drop table #person
drop table #windows

解决方案 »

  1.   

     @d ,@a是不是有一个是null啊
    print 出来看一下
      

  2.   

    没有,得出的结果是
    第一条得出的结果已经是非分@D>@C了
    14 2 3 4 1 bo FrmColor
    @a      @b      @c      @d      @e      @f      @g
      

  3.   

    在怀疑的地方,加print @a 等,查看
      

  4.   

    while @d<=@c
    是这个吧,不是while @d<=@a 吧
    如果while @d<=@c 里没有执行,那就
    在这句之前加个 
    print @d 
    print @c  看看
      

  5.   


    这样的话 @d<=@c 就不会循环了啊
      

  6.   

    就是@D<=@A这个出现问题
      

  7.   

    但是为什么呢? MIN =@D=1而MAX=@C=3的,我就是不明白它为什么直接是@D=4了
      

  8.   

    根据你3楼的结果
    14 2 3 4 1 bo FrmColor 
    @a      @b      @c      @d      @e      @f      @g说明:
    @c=3
    @d=4@d > @c 
    而 你的条件是这个: while @d<=@c   那while 里肯定不能运行啊
      

  9.   

    While @d<=@c    ----@d从1递增到@c=3
    select @e=(select count(*) from rights_table where c_username=@f and c_description=@g)     ----判定有没有记录
    if @e=0
      begin               ----插入数据
       end
    set @d=@d+1         ---加1
    end
    怎么样也看不出问题在那里?