当前所在位置:珠峰网资料 >> 计算机 >> Oracle认证 >> 正文
oracle的or操作符使用错误
发布时间:2011/1/26 16:13:45 来源:www.xue.net 编辑:城市总裁吧

  工作中,发现一处数据计算结果异常,检查原始数据,没有发现问题,再手工计算,和系统计算结果一比,吓一跳:竟然大出了很多倍,于是,开始检查程序,发现有如下调用:

  select tt.BC1610

  from TJ_V_PFI_BC1610_BC1620 tt

  where to_char(tt.tj_date, 'yyyymm') = '200811'

  and tt.regionid = 999

  or tt.regionid in (select b.regionid

  from tj_performance_region_sub b

  where b.type = 0

  and b.parentregionid = 999)

  取出执行发现,结果确实异常,检查分析后发现,竟然是 where 条件的问题,是在条件中用到了 or 操作符,由于粗心的原因,没有注意到它们的关系,引起结果的异常,改成如下所示的SQL,测试正常:

  select nvl(sum(tt.BC1610), 0)

  from TJ_V_PFI_BC1610_BC1620 tt

  where to_char(tt.tj_date, 'yyyymm') = '200811'

  and (tt.regionid = 999

  or tt.regionid in (select b.regionid

  from tj_performance_region_sub b

  where b.type = 0

  and b.parentregionid = 999));

  唯一的区别是,第一句没有将两个 OR 条件括起来。

  工作中,用到 OR 的地方特别多,如果OR和其它的条件是并列的话,一定要注意OR的应用。

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved