The problem is that the query is not returning any rows . . . hence producing a NULL value. Move the isNULL()
outside the subquery:
update destTable
set destField = isnull((select top 1 s.sourceField
from sourceTable s
where <various matches between the destTable table and the s table>
), '');
By the way, normally I would advocate coalesce()
in this situation, because it is the standard. However, the two behave differently, as described by Aaron Bertrand here. In particular, the first argument appears to be evaluated twice, which is quite expensive when it is a subquery. Isnull()
, apparently, does not have this problem.