SQL SERVER [S1000][681] 尝试将不可为 NULL 的列的值设置为 NULL。

这个问题之前就有遇到过。然后忘记怎么解决了。这次记录一下

 

这个问题目前发现原因可能有两个。

1、在字段列表上写复杂的sql然后刚好有where条件中使用了ID=XXX然后XXX又等于空的

2、复杂的关联查询用了多层left join 、union all的。这种可能是和字段列表中又参与计算时。与下面left join的顺序有关。例如

select 
xxxxxx

(case
     when p.decisionId != 0 then (case
                                      when o.orderNo is not null
                                          then aid.orderPrice * dc.exchangeRate / x.cnt
                                      else p.intendingSum / x.cnt end)
     else p.intendingSum / x.cnt end)                            as 订单金额要除于资产数,
xxxxxx
from 
xxxxxx
left join (select count(purchaseNo) as cnt, purchaseNo, ordersno
                          from AssetsInfoDetail
                          group by purchaseNo, ordersno) x on x.purchaseNo = aid.purchaseno and
                                                              ((x.ordersno = aid.ordersno and x.ordersno is not null and
                                                                aid.ordersno is not null) or
                                                               (x.ordersno is null and aid.ordersno is null))
left join Decision dc on dc.id = p.decisionId
xxxxxx
where
xxxxxx

将下面两个left join 调换一下位置就可以正常执行了。

推测可能是由于计算下面代码时dc要比cnt先使用导致?

then aid.orderPrice * dc.exchangeRate / x.cnt

之前有看百度其他资料。说可能与联合查询的表顺序有关

 

 

参考资料:https://www.cnblogs.com/cr-cool/p/13446887.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值