select case cast(rand()*3 as int)
when '0' then '缺货时,请联系我 ' 
when '1' then '缺货时,将有货的商品发出,取消无货商品的订购' 
when '2' then '缺货时,取消此订单'
end 问一下,这个SqServer语句为什么会出现null呢?
rand生成任意小数,然后*3,生成的数应该就在0.1.2三个数之中阿。

解决方案 »

  1.   


    select case cast(rand(3)*10 as int)
    when 0 then '缺货时,请联系我 '  
    when 1 then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when 2 then '缺货时,取消此订单'
    end  
    /*
    (无列名)
    缺货时,将有货的商品发出,取消无货商品的订购
    */应该这样吧,你乘以三仅仅是将这个小数乘以三那假如是0.1呢?乘以三是多少?
      

  2.   

    select case cast(rand()*10 as int)
    when 0 then '缺货时,请联系我 '  
    when 1 then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when 2 then '缺货时,取消此订单'
    end  
    /*
    (无列名)
    缺货时,将有货的商品发出,取消无货商品的订购
    */应该这样吧,你乘以三仅仅是将这个小数乘以三那假如是0.1呢?乘以三是多少?
    更正一下
      

  3.   

    declare @i int 
    set @i = cast(rand()*3 as int)
    select case 
    when @i =0 then '缺货时,请联系我 '  
    when @i =1 then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when @i =2 then '缺货时,取消此订单'
    else convert(char(1),@i)
    end  
      

  4.   

    0.1*3=0.3 不是等于0么?
    现在想要的就是生成0-3啦,我想知道它为啥不可以,
    还想问一下,可以打印它的值吗case里面的内容。可惜我对sql不熟。
      

  5.   

    declare @i int 
    select @i=rand()*3
    select case @i
    when 0 then +'缺货时,请联系我 '  
    when 1 then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when 2 then '缺货时,取消此订单'
    end  
      

  6.   


    select case floor(rand()*3)
    when '0' then '缺货时,请联系我 '  
    when '1' then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when '2' then '缺货时,取消此订单'
    end 
      

  7.   

    不是截断,而是四舍五入了?
    select case cast(rand()*3 as int)
    when '0' then '缺货时,请联系我 '  
    when '1' then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when '2' then '缺货时,取消此订单'
    else '3!'
    end 
      

  8.   

    不是四舍五入吧。我select rand()*3 as int ,执行了许多次,就没看到生成其它数字。select case cast(rand()*3 as int)
    when '0' then '缺货时,请联系我 '  
    when '1' then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when '2' then '缺货时,取消此订单'
    else 'xxxxxxxxx'
    end
    这样的话就会有时执行xxxxxxxx,这就说明了其实rand()*3 as int 生成了除0.1.2之外的其它数字额。为什么呢?如何查看生成 的是多少呢? 为什么会这样呢?sqlserver的断点功能可以查看得到它的数值吗?我试了下,没弄好。可能是我不会用吧。
      

  9.   

    还真是会出现xxxx!
    下面这样的sql,能查到rand()了,但是也 怎么也不会出现xxxx了:select fr,case
    cast(fr *3 as int)
    when '0' then '缺货时,请联系我 '   
    when '1' then '缺货时,将有货的商品发出,取消无货商品的订购'   
    when '2' then '缺货时,取消此订单'
    else 'xxxxxxxxx'
    end
    from (
    select rand() fr
    ) a
      

  10.   

    感觉像是mssql的bug了,下面的sql,前2个字段的结果一直是正常的:
    select cast(rand()*3 as int),rand(),case cast(rand()*2 as int)
    when 0 then '缺货时,请联系我 '   
    when 1 then '缺货时,将有货的商品发出,取消无货商品的订购'   
    when 2 then '缺货时,取消此订单'
    when 3 then '3'
    when null then 'null'
    else 'xxxxxxxxx'
    end
      

  11.   

    情况一:
    select case cast(rand()*3 as int)
    when 0 then '缺货时,请联系我 '  
    when 1 then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when 2 then '缺货时,取消此订单'
    else '*************************'
    end情况二:
    declare @str varchar 
    set @str =  cast(rand()*3 as int)
    select case @str
    when 0 then '缺货时,请联系我 '  
    when 1 then '缺货时,将有货的商品发出,取消无货商品的订购'  
    when 2 then '缺货时,取消此订单'
    else @str + '************************'
    end情况一很容易就出现星号了
    情况二应该不会出现星号
    求大神解
      

  12.   

    解释一下这个问题,我也是从微软论坛得来的一个比较靠谱的解释:
    针对一个不能确定的值(通过RAND()函数),每一个when,rand()*3都会重新计算一次,其实就相当于:
    when cast(rand()*3 as int)=0 then 0
    when cast(rand()*3 as int)=1 then 1
    when cast(rand()*3 as int)=2 then 2
    如果都不成立,那就是else,也就是null了。借助一个表,通过执行计划可以印证这个结论:
    set statistics profile on
    select case cast(rand()*3 as int)
    when 0 then 0 
    when 1 then 1 
    when 2 then 2
    end  
    FROM  [sys].[columns]
    执行计划:
      |--Compute Scalar(DEFINE:([Expr1017]=CASE WHEN CONVERT(int,rand()*(3.000000000000000e+000),0)=(0) THEN (0) ELSE CASE WHEN CONVERT(int,rand()*(3.000000000000000e+000),0)=(1) THEN (1) ELSE CASE WHEN CONVERT(int,rand()*(3.000000000000000e+000),0)=(2) THEN (2) ELSE NULL END END END))如果是一个可以确定的值,那么执行计划会不同:
    select case (5-3)
    when 0 then 0 
    when 1 then 1 
    when 2 then 2 
    end  
    FROM  [sys].[columns]
    执行计划:
      |--Compute Scalar(DEFINE:([Expr1017]=(2)))