請注意,我已經改變了的表和字段的名稱,使這個簡短易懂。SQLServer的更新語句扼流圈空值,即使ISNULL()
我有一個查詢,歸結,談到這一點:
update destTable
set destField = (select top 1 isnull(s.sourceField, '') from sourceTable s
where <various matches between the destTable table and the s table>
);
(我知道的語法的 '更新destTable設置destField ......從destTable d,sourceTable會個... ...'但我不知道如何把「頂部1」到它)
由此我得到的SQLServer 2012特快結果:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'destField', table 'destTable'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
兩個表的所有字段定義爲不能爲空和默認('')。
「top 1」很重要,因爲sourceTable可能有多個匹配的「where」子句。
我查詢了sourceTable的所有行,發現它的所有sourceField值都是非空的。然而我得到了結果。
查詢的性質是,可能有1000個destTable記錄與sourceTable的匹配只會產生300行匹配。其他700個destTable記錄將不會匹配。
我不明白SQLServer對我做了什麼。這個查詢在我上次用MySQL運行時工作得很好。
在此先感謝, 傑羅姆。
把ISNULL(),您告訴我,有我的問題解決了。非常感謝。 –