SQL查询:仅填充空值

SQL查询:仅填充空值,sql,sql-server,Sql,Sql Server,我正在尝试使用SQLServerManagementStudio 2008运行以下查询 update Schema.[ClientNew] set [Notes] = ([Notes]+ char(10)+(Select [Notes] from Schema.[clientOld] where office = 90 and ID = '123456789AM')) where office = 90 and ID= '123456789' 基本上是尝试将我的notes字段中的

我正在尝试使用SQLServerManagementStudio 2008运行以下查询

update Schema.[ClientNew]  
set [Notes] = ([Notes]+ char(10)+(Select [Notes] from Schema.[clientOld]
where  office = 90  and ID = '123456789AM')) where  office = 90  and ID= '123456789' 
基本上是尝试将我的notes字段中的所有数据从ClientHold合并到clientnew中的notes字段

这只是代码的一部分

分离时:

 Select [Notes] from Schema.[clientOld] where  office = 90  and ID = '123456789AM'
为我提供notes字段中的数据

但是当我运行查询时,clientNew表上只显示{null}值

我的问题错了吗??或者在某处写入空值


请告知,

如果
Schema.[ClientNew]
[Notes]
字段中获得
Null
,则更新将无效

这是因为不能使用运算符
+

例如:

这样做:

select null + 'a'
结果将是
null
。 因此,如果表ClientNew获得空值,则

([Notes]+ char(10)+(Select [Notes] from Schema.[clientOld]
where  office = 90  and ID = '123456789AM'))
将返回Null

请尝试添加一个空字符串

(ISNULL([Notes], '') + char(10)+(Select [Notes] from Schema.[clientOld]
where  office = 90  and ID = '123456789AM'))

NULL+anything总是等于NULL,您需要使用COALESCE来防止NULL:

UPDATE    [Schema].ClientNew
SET              Server = COALESCE([Schema].ClientNew, '') + COALESCE([Schema].clientOld, '')
FROM         [Schema].ClientNew INNER JOIN
                      [Schema].clientOld ON [Schema].clientOld.office = [Schema].ClientNew.office AND ID = [Schema].ClientNew.ID
WHERE     (office = 90) AND (ID = '123456789AM')

ClientNew在更新之前在Notes中有空值?是。空值是问题所在。我知道这很愚蠢。谢谢:)