SQL中關於NULL的程式碼

iSQlServer發表於2009-10-14

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

    */

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-616533/,如需轉載,請註明出處,否則將追究法律責任。

相關文章