SQL Server 2008: Why table scaning table when another logical condition is satisfied first?
Consider following piece of code:

    declare @var bit = 0
    
    select * from tableA as A
    where
    1=
    (case when @var = 0 then 1
          when exists(select null from tableB as B where A.id=B.id) 
          then 1
          else 0
    end)

Since variable @var is set to 0, then the result of evaluating searched case operator is 1. In the documentation of case it is written that it is evaluated until first WHEN is TRUE. But when I look at execution plan, I see that tableB is scanned as well.

Does anybody know why this happens? Probably there are ways how one can avoid second table scan when another logical condition is evaluated to TRUE?
SQL Server 2008: Why table scaning table when another logical condition is satisfied first?
===

Consider following piece of code:

    declare @var bit = 0
    
    select * from tableA as A
    where
    1=
    (case when @var = 0 then 1
          when exists(select null from tableB as B where A.id=B.id) 
          then 1
          else 0
    end)

Since variable @var is set to 0, then the result of evaluating searched case operator is 1. In the documentation of case it is written that it is evaluated until first WHEN is TRUE. But when I look at execution plan, I see that tableB is scanned as well.

Does anybody know why this happens? Probably there are ways how one can avoid second table scan when another logical condition is evaluated to TRUE?