现在的位置: 首页 > 综合 > 正文

数据库null值小议

2011年01月31日 ⁄ 综合 ⁄ 共 454字 ⁄ 字号 评论关闭

在数据库的查询过程中,遇到以下问题:
当select * from tKeyword where typeId = case when @typeId < 0 then null else @typeId end order by orderNo desc
这句话是正确的,但是当@typeId < 0不出结果,而数据库中有满足条件typeId=null的数据。
后来进行了以下处理:
if @typeId < 0 then
    select * from tKeyword where typeId is null order by orderNo desc
else
    select * from tKeyword where typeId = @typeId order by orderNo desc
问题得到解决了。进一步简化问题如下:
select * from tKeyword where isnull(typeId,@typeId)=@typeId end order by orderNo desc
显示这是一种更为简化的处理方法。

在数据操作时,一定要注意字段的null的处理方法。
好了,以后有机会在写了。

抱歉!评论已关闭.