Warning: Null value is eliminated by an aggregate or other SET operation.

Null 值会被聚合函数忽略,默认情况下,Sql Server会给出Warning:

Warning: Null value is eliminated by an aggregate or other SET operation.

这个Warning说明Null value 被聚合函数忽略,此时的 SET ANSI_WARNINGS 选项是ON状态,例如,count()函数不会计算null值,min/max函数不会计算null值。如果设置 SET ANSI_WARNINGS OFF ,SQL Server不会抛出 warning 消息。

1,忽略NULL 值

例如,结果是1,正确,但是出现一个Warning。

use tempdb
go 

set ANSI_WARNINGS on 

if object_id('#dt_test','U') is not null
    drop table #dt_test

create table #dt_test
(    
    id int
)

insert into #dt_test
values(1),(null)

select min(id)
from #dt_test

强烈推荐:将ANSI_Warnings选项设置为ON

2,查看当前DB的 ANSI_Warnings 选项的设置

select name,
    database_id,
    is_ansi_warnings_on
from sys.databases
where database_id=db_id()

引用《SET ANSI_WARNINGS》:

As the Warning says, NULLs are being ignored because we are using aggregate function (SUM, AVG). To avoid the warning we can use “set ansi_warnings off” before the script. Here is the modified script.

SET ANSI_WARNINGS OFF

When set to ON, if null values appear in aggregate functions, such as SUM, AVG, MAX, MIN, STDEV, STDEVP, VAR, VARP, or COUNT, a warning message is generated. When set to OFF, no warning is issued. 

SET ANSI_WARNINGS must be ON when you are creating or manipulating indexes on computed columns or indexed views. If SET ANSI_WARNINGS is OFF, CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail

参照文档:

SET ANSI_WARNINGS (Transact-SQL)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悦光阴

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值