string a;
for (int i = 2; i < 7; i++)
{
 string b = "select count (*) from table1 where layer = " +i.ToString();
 a= b+"'";
}大概的意思是这样子的,把每层的总数+'字符组成一个string 返回来,换成用sql语句应该怎么写?或者sql有其它更好的解决办法?谢谢

解决方案 »

  1.   

    declare @layer int,@layerCount int,@returnValue varchar(100)
    set @layer = 2
    set @returnValue = ''while @layer < 7
    begin
    select @layerCount = count(*) from table1 where layer = @layer
    set @returnValue = @returnValue + '_' + convert(varchar,@layerCount) 
    set @layer = @layer + 1
    end
    --因为第一个字符'_'是多出来的,所以截断
    set @returnValue = substring(@returnValue,2,len(@returnValue)-1)
    select @returnValue
      

  2.   

    这个为什么用sql来做呢?  ^0^create table test
    (
    a int
    )
    insert test select 1 union select 2 union select 3
    union select 4 union select 5 union select 6
    union select 7declare @a int,@b varchar(20),
            @str varchar(8000)
    select @a=2,@str=''
    while @a<7
    begin
    set @b=(select count(*) from test where a=@a)
    select @str=@str+@b+'''',@a=@a+1
    end
    print @str
    drop table test
      

  3.   

    ---》select @str=@str+(select rtrim(count(*)) from test where a=@a)+'''',@a=@a+1
    @b拿掉 ^^