当前位置: 代码迷 >> Sql Server >> if的用法解决思路
  详细解决方案

if的用法解决思路

热度:98   发布时间:2016-04-27 16:29:34.0
if的用法
declare   @A   varchar(20),@B   varchar(20)


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

Select   *
from   Table  
where   [email protected]   and   [email protected]

end
else

if(@A <> null)
begin

Select   *
from   Table  
where   [email protected]  

end
else
if(@B <> null)
begin

Select   *
from   Table  
where   [email protected]

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))
  相关解决方案