Wednesday 24 May 2017

SQL Shortcut Comparative "And" and "Or" Operator Conditions

In SQL Server Management Studio (SSMS), the following code returns "OK", suggesting that the evaluation conditions in SQL are shortcutted (ie. as soon as a condition is met, the code doesn't evaluate any subsequent conditions):

select case when 1=2 or 2=2 or 1/0 = 2 then 'ok' else 'no' end

Had all conditions been evaluated, a "divide by zero" error would have occurred.

I've not looked into whether this is standard ANSI SQL behaviour, but thought I'd share in case it is of use or interest to others.

No comments: