日期:2014-05-18  浏览次数:20483 次

if的用法
declare   @A   varchar(20),@B   varchar(20)


if(@A <> null   and   @B <> null)
begin

Select   *
from   Table  
where   A=@A   and   B=@B

end
else

if(@A <> null)
begin

Select   *
from   Table  
where   A=@A  

end
else
if(@B <> null)
begin

Select   *
from   Table  
where   B=@B

end  


上述语句执行成功
但是为什么不能显示出我需要的内容


------解决方案--------------------
<> null 用法改为 is not null
------解决方案--------------------
楼上正解
------解决方案--------------------
SQL里对NULL的处理不能用 <> 来运算,因为他是个空对象,也就是说这个对象可能不存在

所以要用 字段 is not null 或 字段 is null 来做判断 或是用 isnull()函数,帮助里可以查的到。
------解决方案--------------------
安全第一
------解决方案--------------------
你的数据库中的数据确实是你想向的那样的么?
要是数据不对,怎么查也不会对的
------解决方案--------------------
if(@A <> null and @B <> null)
改成=========================


if((@A is not null) and (@B is not null))