读书人

if的用法解决思路

发布时间: 2012-01-19 00:22:28 作者: rapoo

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))

读书人网 >SQL Server

热点推荐