• SET ANSI_NULLS ON


    定在与 Null 值一起使用等于 (=) 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为。

    当 SET ANSI_NULLS 为 ON 时,即使 column_name 中包含空值,使用 WHERE column_name = NULL 的 SELECT 语句仍返回零行。即使 column_name 中包含非空值,使用 WHERE column_name <> NULL 的 SELECT 语句仍会返回零行。

    当 SET ANSI_NULLS 为 OFF 时,等于 (=) 和不等于 (<>) 比较运算符不遵守 ISO 标准。使用 WHEREcolumn_name = NULL 的 SELECT 语句返回 column_name 中包含空值的行。使用 WHERE column_name <>NULL 的 SELECT 语句返回列中包含非空值的行。

    例子:

     -- Create table t1 and insert values.
    CREATE TABLE t1 (a INT NULL,b VARCHAR(40))
    INSERT INTO t1 values (NULL,'HH')
    INSERT INTO t1 values (0,'LL')
    INSERT INTO t1 values (1,'OO')
    GO

    1、

    -- Print message and perform SELECT statements.
    PRINT 'Testing default setting';
    DECLARE @varname int;
    SELECT @varname = NULL;
    SELECT * FROM t1 WHERE a = @varname;
    SELECT * FROM t1 WHERE a <> @varname;
    SELECT * FROM t1 WHERE a IS NULL;
    GO

    输出:

    Testing default setting
    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    a           b
    ----------- ----------------------------------------
    0           LL
    1           OO

    (2 row(s) affected)

    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    2、

    -- SET ANSI_NULLS to ON and test.
    PRINT 'Testing ANSI_NULLS ON'
    SET ANSI_NULLS ON
    GO
    DECLARE @varname int;
    SELECT @varname = NULL;
    SELECT * FROM t1 WHERE a = @varname;
    SELECT * FROM t1 WHERE a <> @varname;
    SELECT * FROM t1 WHERE a IS NULL;
    GO

    输出:

    Testing ANSI_NULLS ON
    a           b
    ----------- ----------------------------------------

    (0 row(s) affected)

    a           b
    ----------- ----------------------------------------

    (0 row(s) affected)

    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    3、

    -- SET ANSI_NULLS to OFF and test.
    PRINT 'Testing SET ANSI_NULLS OFF';
    SET ANSI_NULLS OFF;
    GO
    DECLARE @varname int;
    SELECT @varname = NULL;
    SELECT * FROM t1 WHERE a = @varname;
    SELECT * FROM t1 WHERE a <> @varname;
    SELECT * FROM t1 WHERE a IS NULL;
    GO

    输出:

    Testing SET ANSI_NULLS OFF
    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    a           b
    ----------- ----------------------------------------
    0           LL
    1           OO

    (2 row(s) affected)

    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    指定在与 Null 值一起使用等于 (=) 和不等于 (<>) 比较运算符时采用符合 ISO 标准的行为。

    当 SET ANSI_NULLS 为 ON 时,即使 column_name 中包含空值,使用 WHERE column_name = NULL 的 SELECT 语句仍返回零行。即使 column_name 中包含非空值,使用 WHERE column_name <> NULL 的 SELECT 语句仍会返回零行。

    当 SET ANSI_NULLS 为 OFF 时,等于 (=) 和不等于 (<>) 比较运算符不遵守 ISO 标准。使用 WHEREcolumn_name = NULL 的 SELECT 语句返回 column_name 中包含空值的行。使用 WHERE column_name <>NULL 的 SELECT 语句返回列中包含非空值的行。

    例子:

     -- Create table t1 and insert values.
    CREATE TABLE t1 (a INT NULL,b VARCHAR(40))
    INSERT INTO t1 values (NULL,'HH')
    INSERT INTO t1 values (0,'LL')
    INSERT INTO t1 values (1,'OO')
    GO

    1、

    -- Print message and perform SELECT statements.
    PRINT 'Testing default setting';
    DECLARE @varname int;
    SELECT @varname = NULL;
    SELECT * FROM t1 WHERE a = @varname;
    SELECT * FROM t1 WHERE a <> @varname;
    SELECT * FROM t1 WHERE a IS NULL;
    GO

    输出:

    Testing default setting
    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    a           b
    ----------- ----------------------------------------
    0           LL
    1           OO

    (2 row(s) affected)

    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    2、

    -- SET ANSI_NULLS to ON and test.
    PRINT 'Testing ANSI_NULLS ON'
    SET ANSI_NULLS ON
    GO
    DECLARE @varname int;
    SELECT @varname = NULL;
    SELECT * FROM t1 WHERE a = @varname;
    SELECT * FROM t1 WHERE a <> @varname;
    SELECT * FROM t1 WHERE a IS NULL;
    GO

    输出:

    Testing ANSI_NULLS ON
    a           b
    ----------- ----------------------------------------

    (0 row(s) affected)

    a           b
    ----------- ----------------------------------------

    (0 row(s) affected)

    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    3、

    -- SET ANSI_NULLS to OFF and test.
    PRINT 'Testing SET ANSI_NULLS OFF';
    SET ANSI_NULLS OFF;
    GO
    DECLARE @varname int;
    SELECT @varname = NULL;
    SELECT * FROM t1 WHERE a = @varname;
    SELECT * FROM t1 WHERE a <> @varname;
    SELECT * FROM t1 WHERE a IS NULL;
    GO

    输出:

    Testing SET ANSI_NULLS OFF
    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

    a           b
    ----------- ----------------------------------------
    0           LL
    1           OO

    (2 row(s) affected)

    a           b
    ----------- ----------------------------------------
    NULL        HH

    (1 row(s) affected)

     
     
  • 相关阅读:
    VMware下ubuntu与win8共享文件时/mnt/hgfs目录为空的解决办法
    Flex Array内置排序方法的使用
    Flex 选项卡加载方式简介
    Flash Builder 4.6 基本设置
    Flash Builder 4.6 找不到所需的Adobe Flash Player
    2 python--工具pycharm
    1 python--安装
    安装aix补丁包
    python_day02 上节课知识点回顾
    vue组件局部与全局注册的区别
  • 原文地址:https://www.cnblogs.com/oo_o/p/5394129.html
Copyright © 2020-2023  润新知