关于sql条件语句的问题

MS SQL Server 2008下:

一:获取随机数(0,1,2)------从理论上来说,下面的sql只会产生0,1,2三个随机数(实际运行多次也只发现这三个随机数)。
select  cast(rand()*3 as int)

二:根据第一点实现sql条件语句------同样,下面的sql也只会有三个结果(实际运行的结果中会出现空值)。
select case cast(rand()*3 as int) when '0' then '当前随机数为0'when '1' then '当前随机数为1' when '2' then '当前随机数为2' end

三:解决上面问题的方案有两个:
A方案:
select case cast(rand()*3 as int) when '0' then '当前随机数为0'when '1' then '当前随机数为1' else '当前随机数为else' end
B方案:
declare @num int ,@words varchar(50)
select  @num = cast(rand()*3 as int)
select  @words = (select case @num when '0' then '当前随机数为0'when '1' then '当前随机数为1' when '2' then '当前随机数为2' end)
print   @num
print   @words

四:关于上述问题的疑问
1.关于第二点的那个空值从哪儿来的?
2.解决方案会不会存在什么问题?
3.出现上述问题的原因是?

--求解释!

猜你喜欢

转载自yuyanyan.iteye.com/blog/1537301