日期:2014-05-18  浏览次数:20869 次

sql update 对null字段无法更新?
是这样的,假如一张表table1的结构如下:

uid name memo m1
===================
001 张三 null hahaha



我是这样用的,update table1 set memo=memo+m1 where uid=001
结果是无法更新,也不报错,但如果是update table1 set memo=m1 where uid=001则是可以的。

另外,如果table1中的memo字段是非null,比如是这样的表
uid name memo m1
===================
001 张三 mmmm hahaha
则执行update table1 set memo=memo+m1 where uid=001 就可成功,结果是memo= mmmmhahaha

请问如何解决...


------解决方案--------------------
update table1 set memo=isnull(memo,' ')+m1 where uid=001 

------解决方案--------------------
--
declare @s varchar(10)
declare @s2 varchar(10)
set @s=null
set @s2=@s+'888'
select @s2
set @s2=isnull(@s,'')+'888'
select @s2

/*
---------- 
NULL

(所影响的行数为 1 行)


---------- 
888

(所影响的行数为 1 行)
*/


------解决方案--------------------
SQL code

update table1 set memo=isnull(memo,' ')+m1 where uid=001

------解决方案--------------------
null与任何字符相加,都是null
同样,相加时也是这样,所以在计算中,或者拼合字符串时,一定要注意要使用isnull()函数。
------解决方案--------------------
NULL是一个销毁装置,它与任何的东西操作,无论加减乘除,还是其他什么操作,只要有操作,结果都是NULL!!
------解决方案--------------------
SET CONCAT_NULL_YIELDS_NULL OFF
update table1 set memo=memo+m1 where uid=001 


这样就可以了