I need some information about processing order of the Searched CASE WHEN statement in SQL.
For example:
(CASE
WHEN Cells.Type <> 'UOM' THEN 0
WHEN Template_Colors.Uom_Case = 'ONE' THEN 1
WHEN Template_Colors.Uom_Case = 'MUL' THEN 2
END) AS CELL_WHAT_UOM
I need the result to be always 0, unless Cells.Type <> 'UOM'.
I know that SQL parsers usually proceed from the bottom to the top, but in Oracle the query seems to work correctly, so I suppose that WHEN clauses are processed in the order they are.
It is 100% guaranteed that the 0 case is processed before the others?
Or I have compulsorily to write:
(CASE
WHEN Cells.Type <> 'UOM' THEN 0
WHEN Cells.Type = 'UOM' AND Template_Colors.Uom_Case = 'ONE' THEN 1
WHEN Cells.Type = 'UOM' AND Template_Colors.Uom_Case = 'MUL' THEN 2
END) AS CELL_WHAT_UOM
…?
I need this information about Oracle 10g + SQL server 2008 r2 + Postgre.
If
Cells.Typeis not equal toUOM, you’re guaranteed that the result will be 0.What you are not guaranteed (certainly in SQL Server) is that any expressions in the other branches will not be computed.
So, if you did the following:
You might get a division by zero error.
And, even in your example:
There is no guarantee on the order in which predicates are evaluated, so
Template_Colors.Uom_Casemay still be accessed. Not sure what your concern is around whether this column is accessed or not, so no further advice to offer at the present moment.SQL Server documentation:
Oracle:
PostGre:
Ansi 92 (draft):