“无法插入NULL值”,但是没有NULL值!

Yug*_*ryl 6 null sql-server

这个问题遵循我的另一个问题的答案: 插入与子查询表相关的输出

Person.LastName 列有一个 NOT NULL 约束。当我执行此代码时:

CREATE TABLE tempIDs
( PersonId INT, 
  FinancialInstitutionId INT
);

MERGE INTO Person 
USING FinancialInstitution AS fi
  ON 1 = 0
WHEN NOT MATCHED THEN
  INSERT (CreationDate, AdministrativeStatus, LastName, Street1, Number1, City1, State1, PostCode1, CountryId1, WorkDirectPhone1, Fax1, Email1)
  VALUES (GetDate(), 'Legal', fi.Name, fi.Street, fi.Number, fi.City, fi.[State], fi.PostCode, fi.CountryId, fi.PhoneNumber, fi.Fax, fi.Email)
OUTPUT inserted.Id, fi.Id INTO tempIDs;

UPDATE fi
SET fi.PersonId = t.PersonId
FROM FinancialInstitution AS fi
  JOIN tempIDs AS t
    ON fi.Id = t.FinancialInstitutionId ;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Cannot insert the value NULL into column 'LastName', table 'Person'; column does not allow nulls. UPDATE fails.
Run Code Online (Sandbox Code Playgroud)

问题是没有 FinancialInstitution.Name 是 NULL。

SELECT Name FROM FinancialInstitution WHERE Name = NULL
Run Code Online (Sandbox Code Playgroud)

这不返回任何行。此外,如果我用一个值('A Last Name')替换 fi.Name 请求有效。

Mar*_*son 12

NULL不是一个值。有些东西不能 '=' NULL

你要:

SELECT Name FROM FinancialInstitution WHERE Name IS NULL
Run Code Online (Sandbox Code Playgroud)

  • 嘘** 为我感到羞耻!现在我记得我的老师几年前解释过!这就是 COALESCE 变得有用的地方! (5认同)