当为 NULL 时的情况

CASE WHEN IS NULL

我想将 PlaceTypeId = 18 放在 PlaceType.PlaceTypeId 列中(如果它为空)。我尝试使用这种情况 null 但它不起作用。难道我做错了什么?

SELECT DoctorsAddress.AddressId, DoctorsAddress.Address, PostalCode.Locality, 
  (CASE WHEN (PlaceType.PlaceTypeId IS NULL) THEN 18 ELSE    
   DoctorsAddress.PlaceTypeId END) AS TipoLocal, 
  (CASE WHEN (placeofvisitquery.placeofvisit IS NULL) THEN '' ELSE
   DoctorEnterpriseDetails.Schedule END) AS Schedule, 
  PostalCode.PostalCode, DoctorsAddress.DoctorId, 
  (CASE WHEN (placeofvisitquery.placeofvisit IS NULL) THEN '' ELSE 'X' END) AS teste
FROM  DoctorsAddress 
 INNER JOIN PlaceType ON DoctorsAddress.PlaceTypeId = PlaceType.PlaceTypeId
 INNER JOIN PostalCode ON DoctorsAddress.PostalCode = PostalCode.PostalCodeId 
 INNER JOIN DoctorEnterpriseDetails ON DoctorsAddress.DoctorId = DoctorEnterpriseDetails.DoctorId 
 LEFT OUTER JOIN DoctorRepresentative ON
   DoctorEnterpriseDetails.EnterpriseId = DoctorRepresentative.EnterpriseId AND 
   DoctorsAddress.DoctorId = DoctorRepresentative.DoctorId AND                       
   DoctorsAddress.AddressId = DoctorRepresentative.placeofvisit 
 LEFT OUTER JOIN placeofvisitquery ON 
   DoctorsAddress.DoctorId = placeofvisitquery.DoctorId AND
   DoctorsAddress.AddressId = placeofvisitquery.placeofvisit
WHERE (DoctorsAddress.DoctorId = @param2) AND (DoctorEnterpriseDetails.EnterpriseId = 26)

我认为你的问题是你在 DoctorsAddress.PlaceTypeIdPlaceType.PlaceTypeId 之间使用了 INNER JOIN,所以这将连接 with a value 中的每一行(并且不包括空值) DoctorsAddressPlaceType.

中的每个对应行

所以您永远不会得到任何实际包含 PlaceType.PlaceTypeId IS NULL 的行。 如果你更换

,你可以拥有这些

INNER JOIN PlaceType ON DoctorsAddress.PlaceTypeId = PlaceType.PlaceTypeId

LEFT OUTER JOIN PlaceType ON DoctorsAddress.PlaceTypeId = PlaceType.PlaceTypeId

然后(如果 DoctorsAddress.PlaceTypeId 为空)您将从 DoctorsAddress 中选择行,但不会找到 PlaceType 中的相应行,因为 DoctorsAddress.PlaceTypeId IS NULL并且由于您无法与 NULL 进行比较,因此 PlaceType 的每个成员也将是 NULL.