数据库高手进 问题神奇

这是一个语句

select 
case  cast(rand()*10 as int)%4 
when 0 then 0
when 1 then 1 
when 2 then 2 
when 3 then 3 
else 4 end

这样执行的结果经常出现一个  4   ?? 为什么啊

 

这样写 就不会出现4   select cast(rand()*10 as int)%4

 

if(abs(checksum(newid()))%4='')
begin
print '123'
end

 

if(abs(checksum(rand()))%4='')
begin
print '123'
end

以上2个结果也经常出现 123 为什么了。

 

把问题发到了首页,急切想找到答案哦。

 

问了好多了。还是没有答案。


梦里花落0921
浏览 467回答 2
2回答

九州编程

select case  cast(rand()*10 as int)%4 when 0 then 0when 1 then 1 when 2 then 2 when 3 then 3 else 4 end相当于 if (cast(rand()*10 as int)%4 == 0)  //print 0else if (cast(rand()*10 as int)%4 == 1)     //print 1 else if (cast(rand()*10 as int)%4 == 2)     //print 2 else if (cast(rand()*10 as int)%4 == 3)     //print 3else   //print 4每次都产生 了随机数,所以才会出现4使用DECLARE @d intSET @d = cast(rand()*10 as int)%4 SELECT case  @dwhen 0 then 0when 1 then 1 when 2 then 2 when 3 then 3 else 4 end就没有问题了
打开App,查看更多内容
随时随地看视频慕课网APP