求-10~10之间的正整数的个数我用 declare @a int
set @a=-10
if (@a between -10 and 10)
    begin
        while (@a<=10)
            begin
                select @a,case when @a>0 then 1
                        else 0
                end as 'Y'
                set @a=@a+1
            end
    end查询出了如下结果
   Y
----------- -----------
-10         0(1 行受影响)            Y
----------- -----------
-9          0(1 行受影响)            Y
----------- -----------
-8          0(1 行受影响)            Y
----------- -----------
-7          0(1 行受影响)            Y
----------- -----------
-6          0(1 行受影响)            Y
----------- -----------
-5          0(1 行受影响)            Y
----------- -----------
-4          0(1 行受影响)            Y
----------- -----------
-3          0(1 行受影响)            Y
----------- -----------
-2          0(1 行受影响)            Y
----------- -----------
-1          0(1 行受影响)            Y
----------- -----------
0           0(1 行受影响)            Y
----------- -----------
1           1(1 行受影响)            Y
----------- -----------
2           1(1 行受影响)            Y
----------- -----------
3           1(1 行受影响)            Y
----------- -----------
4           1(1 行受影响)            Y
----------- -----------
5           1(1 行受影响)            Y
----------- -----------
6           1(1 行受影响)            Y
----------- -----------
7           1(1 行受影响)            Y
----------- -----------
8           1(1 行受影响)            Y
----------- -----------
9           1(1 行受影响)            Y
----------- -----------
10          1(1 行受影响)
结果是分开的,不能做sum操作,请问怎么把结果合并 我用union all 老出错还有就是
@Value % 2 == 0? "even":"odd"
怎样查询
我在前面加select ,提示'=' 附近有语法错误。
谢谢

解决方案 »

  1.   


    create table #t(id int,yesno int)declare @a int
    set @a=-10
    if (@a between -10 and 10)
    begin
            while (@a<=10)
            begin
                    insert into #t (id,yesno)
                    select @a,case when @a>0 then 1 else 0 end as 'Y'                set @a=@a+1
            end
    endselect * from #tdrop table #t
      

  2.   


    --用临时表,把数据逐条保存到临时表中,最后select 查询。create table #t(id int,yesno int)declare @a int
    set @a=-10
    if (@a between -10 and 10)
    begin
            while (@a<=10)
            begin
                    insert into #t (id,yesno)
                    select @a,case when @a>0 then 1 else 0 end as 'Y'                set @a=@a+1
            end
    endselect * from #tdrop table #t
      

  3.   

    感谢你的回答,是正确的,同时希望回答下这个
    @Value % 2 == 0? "even":"odd"
    怎样查询
    我在前面加select ,提示'=' 附近有语法错误。
      

  4.   

    @Value % 2 == 0? "even":"odd"
    -----------------------------------这种问号表达式是前台的语法。