Does SQL have both a short-circuit and a hierarchical multi-evaluation syntax?
Example of short-circuit assignment. Here decision gets the FIRST *** clause when … is true:
Short-circuit
case
when (...) then (***)
when (...) then (***)
when (...) then (***)
else (...)
end as decision
Example of hierarchical assignment. Here decision gets LAST *** expression when … is true.
Hierarchical
if (...) then (decision = ***) end
if (...) then (decision = ***) end
if (...) then (decision = ***) end
Inter-Conversion
It is clear that reversing the ordering of the expressions switches between hierarchical and short-circuit. I’m wondering whether SQL also has a construct that would assign the LAST tr expression?
Test Case:
The following gives a very simple toy example:
select
flag1, flag2, flag3,
case
when flag1=1 and flag2=0 then 'LEFT'
when flag1=0 and flag2=0 then 'NONE'
when flag2=0 and flag3=1 then 'RIGHT'
end as decision
FROM
( select
1 as flag1, 0 as flag2, 1 as flag3
-- from dual -- if you use Oracle
) tmp ;
Short-circuit SQL would return ‘LEFT’
An hierarchical assignment would return ‘RIGHT’.
Edit: You can run this example in SQL-Fiddle by clicking on the link. (Thanks to ypercube for pointing out that useful site! (+1))
There is no expression that would do the Hierarchal assignment in one select but it can be achieved just like any other programming language through multiple if statements