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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM