关于NULL的代码

来源:互联网 发布:cosplay软件绅士开关 编辑:程序博客网 时间:2024/05/21 20:21

    set nocount on

 

--########### 工具默认情况下对字段NULL的处理  ###########--

    --默认工具为on

    select ansi_null_dflt_on

    from sys.dm_exec_sessions

    where session_id=@@spid

    /*

    ansi_null_dflt_on

    -----------------

    1

    */

    --或者执行下面查询,结果也为

    select GETANSINULL()

 

 

    --创建测试表,不指定name列是否可为空

    create table tb_A (id int not null, name varchar(20))

 

    --执行如下插入操作,一切正常

    insert into tb_A (id) values (1)

    select * from tb_A

    /*

    id          name

    ----------- --------------------

    1           NULL

    */

 

    --清理测试表

    drop table tb_A

 

--########### 引擎默认情况下对字段NULL的处理  ###########--

    --默认引擎为off

    set ansi_null_dflt_off on

 

    --查看当前回话的ansi_null_dflt_on设置

    select ansi_null_dflt_on

    from sys.dm_exec_sessions

    where session_id=@@spid

    /*

    ansi_null_dflt_on

    -----------------

    0

    */

   

    --创建测试表,不指定name列是否可为空

    create table tb_B (id int not null, name varchar(20))

 

    --执行如下插入操作,会报错

    insert into tb_B (id) values (1)

    /*

    消息515,级别16,状态2,第1

    不能将值NULL 插入列'name',表'IMTiange.dbo.tb_B';列不允许有空值。INSERT 失败。

    语句已终止。

    */

 

    --查看系统视图COLUMNSis_nullable列结果为No

    select is_nullable

    from INFORMATION_SCHEMA.COLUMNS

    where table_name='tb_B' and column_name='name'

    /*

    is_nullable

    -----------

    NO

    */

 

    --或者查看列的属性AllowsNull,为表示不允许为空

    select COLUMNPROPERTY(object_id('tb_B'), 'name', 'AllowsNull')

 

    --清理测试表

    drop table tb_B

 

 

--###########  字符串连接中的NULL处理  ###########--

    --设置concat_null_yields_nullon

    set concat_null_yields_null on

    select concat_null_yields_null

    from sys.dm_exec_sessions

    where session_id=@@spid

    /*

    concat_null_yields_null

    -----------------------

    1

    */

 

    --查询结果为NULL

    select 'this is ['+NULL+'] result' as result

    /*

    result

    ---------

    NULL

    */

 

    --设置concat_null_yields_nulloff,结果不为null

    set concat_null_yields_null off

    select 'this is ['+NULL+'] result' as result

    /*

    result

    ------------------

    this is [] result

    */

 

 

 

--###########  NULL值的比较  ###########--

    --设置并查看设置结果

    set ansi_nulls on

    select ansi_nulls

    from sys.dm_exec_sessions

    where session_id=@@spid

 

    --测试情况:is null为真,=null为假

    declare @i int

    set @i=null

 

    select (case when @i IS null then 'True' else 'False' end) as [is null],

        (case when @i = NULL then 'True' else 'False' end) as [=null]

    /*

    is null =null

    ------- -----

    True    False

    */

 

    --设置为off后,测试情况:is null为真,=null为真

    set ansi_nulls off

 

    declare @i int

    set @i=null

 

    select (case when @i IS null then 'True' else 'False' end) as [is null],

        (case when @i = NULL then 'True' else 'False' end) as [=null]

    /*

    is null =null

    ------- -----

    True    True

    */