Is "OR" in SQL Server short circuit?

Does the "OR" companion work like vb. I mean if he checks everyone or if the first lie

as

DECLARE @var1 bit;
SET @var1=1
DECLARE @var2 bit;
SET @var2=1

(@var1=1 or @var2=1)
AND ...

So, @var is 1, we don’t need to check var2, but sql server will check?

About query optimization (and server memory)

tank you

+3
source share
2 answers

For the most part, yes, SQL Server uses a short circuit for the AND and OR statements, but sometimes the operations are not performed in the order you expect. This is fairly easy to verify using the following:

SELECT '"Divide By Zero" error' AS [test] WHERE (1 / 0 = 1)

SELECT 'NO "Divide By Zero" error' AS [test] WHERE (1 = 1) OR (1 / 0 = 1)

Here you can find a more detailed analysis: Gianluca Sartori: http://www.sqlservercentral.com/articles/T-SQL/71950/

+2

Microsoft Connect:

. , SQL Server , , ANSI SQL . . , CASE SQL Server, :

    ( 1 )     ( 2, )    ...    end = 1

CASE , WHEN/THEN .

0

All Articles