Sql与oracle中null值(四)

2014-11-24 12:59:27 · 作者: · 浏览: 4
T CONCAT_NULL_YIELDS_NULL
{ ON | OFF } 来控制 null 与其它字符串连接的行为。
当 SET CONCAT_NULL_YIELDS_NULL 为 ON 时,串联空值与字符串将产生 NULL 结果。例如, SELECT 'abc' + NULL 将生成 NULL 。
当 SET CONCAT_NULL_YIELDS_NULL 为 OFF 时,串联空值与字符串将产生字符串本身(空值作为空字符串处理)。例如, SELECT 'abc' + NULL 将生成 abc 。
如果未指定 SET CONCAT_NULL_YIELDS ,则应用 CONCAT_NULL_YIELDS_NULL 数据库选项的设置。
注:在 SQL Server 的未来版本中, CONCAT_NULL_YIELDS_NULL 将始终为 ON ,而且将该选项显式设置为 OFF 的任何应用程序都将产生一个错误。
c. 变量的默认值与 null 值
命名一个变量后,如果没有给它赋初始值,它的值就是 null 。有时候需要注意初始 null 值和通过 select 语句给变量后期赋 null 的区别。因为此 ‘null’ 非彼 ‘null’ 。
d. 子查询中的 null
子查询中出现的 null 值经常会被我们忽视,先查看下面的例子。

e. Case 语句中的 null
Case 中的 when 语句注意不要写成 when null, 否则得不到想要的结果。
下面的第 1 条 sql 错误, 2 , 3 正确。


f. 与 null 相关的函数 ISNULL ISNULL 检测表达式是否为 NULL ,如果是的话替换 NULL 值为另外一个值 COALESCE COALESCE 函数返回指定表达式列表的第一个非 NULL 值 NULLIF 当指定的两个表达式有相同值的时候 NULLIF 返回 NULL 值,否则返回第一个表达式的值
示例 1 : set ansi_nulls on
declare @test1 nvarchar ( 10)

if ( @test1 = null)
select 1
else
select 2
---------------------------------------------------------------------
结果返回 2

示例 2 : set ansi_nulls off
declare @test1 nvarchar ( 10)

if ( @test1 = null)
select 1
else
select 2
---------------------------------------------------------------------
结果返回 1

示例 3 :
set ansi_nulls on
select * from dbo. cassaba_null where column2 != null
---------------------------------------------------------------------
无记录返回

示例 4 :
set ansi_nulls off
select * from dbo. cassaba_null where column2 != null
---------------------------------------------------------------------
返回第 2 , 3 条记录


如果不希望每次都判断 null, 可以使用 isnull 函数来达到每次把 null 自动替换为空字符串进行连接的效果。
示例 5 :
declare @test nvarchar ( 50)
select isnull ( @test, '' ) + 'extend'
go

select column1 + isnull ( column2, '' ) as column3 from dbo. cassaba_null
go
---------------------------------------------------------------------


示例 6 :
declare @test nvarchar ( 50)
-- 无符合条件的语句 , 保持默认值 null
select @test= column2 from dbo. cassaba_null where column1 = '4'
-- 有符合条件的语句,返回的栏位值为 null ,并且赋给 @test
select @test= column2 from dbo. cassaba_null where column1 = '1'
如果后面的代码使用 @test 的值是否为 null 来判断有没有匹配的记录,则可能发生错误。碰到这种状况,我们同样可以使用 isnull 函数来避免这个问题。
select @test= isnull ( column2, '' ) from dbo. cassaba_null where column1 = '1'
如上面的语句,即使有匹配的记录返回 null, 也会变成空字符串赋给 @test 了。这样就把两种情况区分开了。
示例 7 : set ansi_nulls off
select * from cassaba_null a where a. column2 = ( select b. column2 from dbo. cassaba_null b where b. column1 = 1)
---------------------------------------------------------------------
不管上面 ansi_nulls 设置为 on 还是 off ,始终没有记录返回。我们修改一下查询语句:
select * from cassaba_null a where a. column2 in ( select b. column2 from dbo. cassaba_null b where b. column1 = 1)
这样,如果 ansi_nulls 设置为 on , 则没有记录返回。 如果设置为 off ,则会返回一条记录。
对于这种状况,如果我们确定不需要返回 null 值记录,则使用下面的 sql 语句:
select * from cassaba_null a where a. column2 in( select b. column2 from dbo. cassaba_null b where b. column1 = 1
and b. column2 is not null)

反之,使用下面的语句:
select * from cassaba_null a where a. column2 in( select b. column2 from dbo. cassaba_null b where b. column1 = 1
and b. column2 is not null) or a. column2 is null


10 Sql与oracle中null值的不同

a.在SQL Server中与oracle正相反,NULL值会被认为是一个无穷小的值,所以如果按照升序排列的话,则会被排在最前面
b. SQL Server和Oracle中对插入数据值包含空的处理有所差异,在SQL Server中,我们可以把表字段设计为非空,但我们仍然可以通过下面语句执行插入操作:
INSERT INTO Table (TestCol) VALUES(‘’)
其中的TestCol字段,在设计的时候,已经被设计为NOT NULL在sql server中,null和空格是不同的,也就是说,上面的语句插入的是一个空,但并不是NULL,只有当我们的插入语句中没有该字段的时候,才会被认为违反非空的条件约束,如果把NUL