This teaser was posted by my friend George on Tek Tips and I am posting it here after I asked for his permission. the main reason I am posting it here is because I want to get some opinions from the experts.
Take a look at these two queries
If
1/0 = 10 And 1/1 = 0
Select 'True' As Query1
Else
Select 'False' As Query1
If 1/0 = 10 And sqrt(1)=0
Select 'True' As Query2
Else
Select 'False' As Query2
The first one runs, the second one does not. It looks like the engine doesn't even look at the 1/0 because the 1/1 = 0 is false anyway. The second query is not using a constant but a function and the optimizer actually has to run the statement and it bombs out. So what do you think, am I right?
Now change the first query to this
If
1/0 = 10 or 1/1 = 0
Select 'True' As Query1
Else
Select 'False' As Query1
And? Does this also happen in other languages like this?
There is some more....:-) Switch the if statement around.
If
1/0 = 10 And sqrt(1)=0
Select 'True'
Else
Select 'False'
-- Same query with the order changed.
If
sqrt(1)=0 And 1/0 = 10
Select 'True'
Else
Select 'False'
Now let's make the AND an OR
If
sqrt(1)=1 or 1/0 = 10
Select 'True'
Else
Select 'False'
If
1/0 = 10 or sqrt(1)=1
Select 'True'
Else
Select 'False'
Very interesting indeed.