【发布时间】:2014-10-05 19:44:26
【问题描述】:
我正在使用这个脚本来生成一些数据
select case abs(checksum(newid())) % 5 + 1
when 1 then 'excellent'
when 2 then 'good'
when 3 then 'fair'
when 4 then 'poor'
when 5 then 'failing'
end
它返回这些值:excellent,good,fair,poor,failing,null
我以为我得到了1,2,3,4,5 之外的结果,导致null 从案例中返回。所以我执行了这个脚本:
select abs(checksum(newid())) % 5 + 1
它只返回这些值:1,2,3,4,5
假设由于一些异常的统计现象(因为我运行了语句 > 100 次)在调试期间我没有得到 0 或 6,那么还有其他一些问题导致了这种情况。
【问题讨论】:
-
顺便说一句,
CASE是一个表达式,而不是一个语句。也许值得一读(出于这些和其他原因):Dirty Secrets of the CASE Expression.
标签: sql sql-server random case