关于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 失败。
语句已终止。
*/
--查看系统视图COLUMNS的is_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_null为on
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_null为off,结果不为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
*/
- 关于NULL的代码
- 关于代码 "object = null" 的讨论
- 关于java的null
- 简述一个关于null的理解误区:null不等于null
- 关于SQL 的NULL 值
- 关于java 中 的 null。
- 关于NULL的一些问题?
- 【Oracle】关于null的讨论
- freemarker关于null的问题
- 关于null的一些整理
- 关于null的使用建议
- freemarker关于null的问题
- 关于null+2的问题
- Oracle 关于null的函数
- 关于List的[]或者[null]
- 关于输出null对象,以及try/catch/finally比较好玩的一段代码
- 关于null != a 与 a != null 的区别
- 关于IOS开发中(null)与<null>的处理
- 这里的博客实在不方便,以后都在新浪也放一个博客。新浪http://blog.sina.com.cn/eggoo为主,这里作为镜像。
- 项目管理生命周期相关文档
- 读书Flex3
- reactos操作系统实现(103)
- 如何启动和关闭MySQL
- 关于NULL的代码
- ARP报文伪造网关
- 数据库的配置和页面调用
- SNS
- 要做的事情
- gcc生成静态库和动态库
- 看了对你必定有好处!!!快看!!
- Weblogic启动时报错:HotSpot Virtual Machine Error
- 英语发音规则