COALESCE or CASE more efficient and/or standard

时间秒杀一切 提交于 2019-12-31 01:57:07

问题


In terms of x compared to y.

  • Does x comply with sql standards better? [apologies if subjective]
  • Is x more efficient than y?
  • Or are these scripts completely different and to be used in different contexts?

x

    SELECT * 
    FROM   a 
           INNER JOIN b ON
           COALESCE(b.columntojoin, b.alternatecolumn) = a.columntojoin

y

    SELECT * 
    FROM   a 
           INNER JOIN b ON
           (case when b.columntojoin is null then b.alternatecolumn else b.columntojoin end) = a.columntojoin

回答1:


COALESCE is essentially a shorthanded CASE statement.

Both are exactly the same.

There is also ISNULL in SQL Server (differs in other DBMSs), but that's actually a non-standard feature and is actually more limited that COALESCE.




回答2:


In this case I would use COALESCE ( which provides more levels than ISNULL) rather than the CASE stement.

The CASE statement seems a bit bulky here, seeing as you are only checking for NULLs anyway.



来源:https://stackoverflow.com/questions/15523000/coalesce-or-case-more-efficient-and-or-standard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!