定义是这样说的:
当SET ANSI_NULLS为ON时,即使column_name中有NULL值,使用WHERE column_name = NULL的SELECT语句也会返回零行。即使column_name中存在非NULL值,使用WHERE column_name <> NULL的SELECT语句也会返回零行。
这是否意味着此查询中将不包括空值?
SELECT Region
FROM employees
WHERE Region = @region
或者,ANSI_NULL
只关心像这样的查询(其中WHERE
包含特定的单词NULL
)?
SELECT Region
FROM employees
WHERE Region = NULL
发布于 2017-12-14 02:55:51
如果ANSI_NULLS设置为" on“,并且如果我们在编写select语句时对NULL列值应用=,<>,那么它将不会返回任何结果。
示例
create table #tempTable (sn int, ename varchar(50))
insert into #tempTable
values (1, 'Manoj'), (2, 'Pankaj'), (3, NULL), (4, 'Lokesh'), (5, 'Gopal')
在上设置ANSI_NULLS
select * from #tempTable where ename is NULL -- (1 row(s) affected)
select * from #tempTable where ename = NULL -- (0 row(s) affected)
select * from #tempTable where ename is not NULL -- (4 row(s) affected)
select * from #tempTable where ename <> NULL -- (0 row(s) affected)
将ANSI_NULLS设置为关闭
select * from #tempTable where ename is NULL -- (1 row(s) affected)
select * from #tempTable where ename = NULL -- (1 row(s) affected)
select * from #tempTable where ename is not NULL -- (4 row(s) affected)
select * from #tempTable where ename <> NULL -- (4 row(s) affected)
发布于 2012-03-19 16:12:50
如果@Region
不是null
值(假设是@Region = 'South'
),则无论ANSI_NULLS的值是什么,它都不会返回区域字段为null的行。
只有当@Region
的值为null
时,ANSI_NULLS才会有所不同,也就是说,当您的第一个查询实质上变成了第二个查询时。
在这种情况下,ANSI_NULLS ON将不会返回任何行(因为null = null
将生成未知的布尔值(也称为null
))和ANSI_NULLS OFF将返回Region字段为null的所有行(因为null = null
将生成true
)
发布于 2017-08-02 17:29:00
将ANSI_NULLS设置为ON
它返回表中的所有值,包括空值
将ANSI_NULLS设置为关闭
当列包含空值时结束
https://stackoverflow.com/questions/9766717
复制相似问题