简体   繁体   中英

Is querying for IS NULL necessary in this CASE expression?

I've got this case statement:

CASE 
  WHEN SecondScriptKind_ID IS NULL THEN 0
  WHEN SecondScriptKind_ID IN (@SqlQuery, @SqlScript) THEN 1
  ELSE 0 
END

Is the WHEN SecondScriptKind_ID IS NULL necessary in this case?

Both @SqlQuery and @SqlScript are guaranteed to contain values (NOT NULL).

When ANSI_NULLS is set on , it is not necessary: even if @SqlQuery or @SqlScript happen to be NULL , the IN is not going to match a NULL SecondScriptKind_ID to that list. In other words, the condition

NULL IN (NULL, 123)

evaluates to NULL . The only reason to keep an explicit NULL check there would be if you wanted to return a different number for NULL , say, a negative 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM