该文档指出,数据库 ANSI_NULLS 标志控制在某些方面与 null 进行比较的行为。
我正在检查此堆栈溢出帖子以检查如何确定(未设置)此标志的值。有趣的是,并非所有答案似乎都对我有用。
我的测试查询:
create table x(id int,txt nvarchar(max))
insert x(id,txt) values (1,'not null here'),(2,null)
select * from x
--query 1
select * from x where txt=null
--query 2
select * from x where txt<>null
返回的输出是两个空结果集。由此,我可以从逻辑上推断出数据库中的 ANSI_NULLS 为 ON。
现在,检查选项:
1
select databasepropertyex('MyDatabaseName', 'IsAnsiNullsEnabled')
返回 0。我没想到会这样。
2
DECLARE @options INT
SELECT @options = @@OPTIONS
IF ( (32 & @options) = 32 ) PRINT 'ANSI_NULLS'
打印“ANSI_NULLS”。预期的。
3
SELECT is_ansi_nulls_on FROM sys.databases WHERE name = 'MyDatabaseName'
返回 0。我没想到会这样。
4
dbcc useroptions
结果集包括 [Set Option]='ansi_nulls' 和 [Value]='Set' 的行。预期的。
为什么选项 1 和 3 会给我这个结果?
我的@@version 是:
Microsoft SQL Server 2017 (RTM-GDR) (KB4505224) - 14.0.2027.2 (X64)
Jun 15 2019 00:26:19
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)