我创建了这个MERGE语句,它从Temp获取数据并将其放入Word表中。这是Temp的模式:
CREATE TABLE [dbo].[Temp] (
[Word] VARCHAR (MAX) NULL,
[SFI] REAL NULL,
[U] INT NULL,
[D] REAL NULL
);
这是MERGE:
MERGE [dbo].[Word] as target
USING [dbo].[Temp] as source
ON target.[WordId] = source.[Word]
WHEN MATCHED THEN
UPDATE SET [NAWL963SFI] = source.[SFI], [NAWL963U] = source.[U], [NAWL963D] = source.[D]
WHEN NOT MATCHED THEN
INSERT ([WordId],[NAWL963SFI],[NAWL963U], [NAWL963D], [NAWL963])
VALUES (source.[Word],source.[SFI],source.[U], source.[D], 1);
当它运行时,它给我一个错误说“
Msg 515, Level 16, State 2, Line 41
Cannot insert the value NULL into column 'WordId', table 'ab.dbo.Word'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
我检查过Temp
中没有空值 SELECT count(*) from Temp where Word = null;
有没有人对我如何调试这个有任何建议,或者我合并的方式有什么问题?
答案 0 :(得分:2)
嗯,您没有正确搜索NULL
,列永远不会等于NULL
,您必须使用IS NULL
和{{ 1}}检查列是否为空:
IS NOT NULL
尝试查找此行
SELECT count(*) from Temp where Word is null;
对此:
USING [dbo].[Temp] as source