标签:
强烈推荐:将选项 ANSI_NULLS 设置为On,在与Null值比较时使用 is null 或 is not null
查看和设置该选项的值
SET ANSI_NULLS ON --SET ANSI_NULLS OFF DECLARE @ANSI_NULLS VARCHAR(3) = ‘OFF‘; IF ( (32 & @@OPTIONS) = 32 ) SET @ANSI_NULLS = ‘ON‘; SELECT @ANSI_NULLS AS [ANSI_NULLS Option];
When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name.
When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the ISO standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL.
When SET ANSI_NULLS is ON, all comparisons against a null value evaluate to UNKNOWN. When SET ANSI_NULLS is OFF, comparisons of all data against a null value evaluate to TRUE if the data value is NULL. If SET ANSI_NULLS is not specified, the setting of the ANSI_NULLS option of the current database applies.
For a script to work as intended, regardless of the ANSI_NULLS database option or the setting of SET ANSI_NULLS, use IS NULL and IS NOT NULL in comparisons that might contain null values.
SET ANSI_NULLS must also be ON when you are creating or changing indexes on computed columns or indexed views. If SET ANSI_NULLS is OFF, any CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail. SQL Server will return an error that lists all SET options that violate the required values. Also, when you execute a SELECT statement, if SET ANSI_NULLS is OFF, SQL Server will ignore the index values on computed columns or views and resolve the select operation as if there were no such indexes on the tables or views.
ANSI_NULLS is one of seven SET options that must be set to required values when dealing with indexes on computed columns or indexed views.The options ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, QUOTED_IDENTIFIER, and CONCAT_NULL_YIELDS_NULL must also be set to ON, and NUMERIC_ROUNDABORT must be set to OFF.
当ANSI_NULLS 为ON时,遵循SQL92的标准,只能使用IS NULL 来判断值是否为NULL, 而不能使用=或<>来与NULL做比较,任何值包括NULL值与NULL值做=或<>运算都得到FALSE。
当ANSI_NULLS为OFF时,将不再遵循SQL92标准,可以使用=和<>来与NULL做BOOL运算。
示例:
if object_id(‘dt_test‘) is not null drop table dbo.dt_test create table dbo.dt_test ( id int ) insert into dbo.dt_test values(1),(null) --当SET ANSI_NULLS OFF,NULL和Null是相同的,Null和非Null值是不相等的, SET ANSI_NULLS OFF select * from dbo.dt_test where id =null --当SET ANSI_NULLS ON,NULL和任何值(包括NUll)比较,都返回False --必须使用is null 来判断null 或is not null来判断非Null SET ANSI_NULLS ON select * from dbo.dt_test where id=null select * from dbo.dt_test where id is null
关于NULL的延伸:
1. 聚合函数COUNT,只有SELECT COUNT(*)和SELECT COUNT(1) 会将为NULL的行计算在内,SELECT COUNT(ID)会忽略ID列为NULL的行
2. 除COUNT外的其他聚合函数,计算时不考虑为NULL的行
3. CHECK CONSTRAINT,在列允许为NULL的条件下,为NULL的行将不受CHEKC CONSTRAINT的限制,例如有CHECK CONSTRAINT条件为C1>10,对C1列插入NULL值或更新为NULL不会违反CHECK CONSTRAINT.
4. 在GROUP BY 的时候,会将所有NULL归为一组
参照文章
http://www.cnblogs.com/TeyGao/p/3521657.html
https://msdn.microsoft.com/zh-cn/library/ms188048.aspx
标签:
原文地址:http://www.cnblogs.com/ljhdo/p/4873960.html