Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1079239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:50:22+00:00 2026-05-16T21:50:22+00:00

Consider following piece of code: declare @var bit = 0 select * from tableA

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:50:22+00:00Added an answer on May 16, 2026 at 9:50 pm

    Because the plan that is compiled and cached needs to work for all possible values of @var

    You would need to use something like

    if (@var = 0)
    select * from tableA 
    else
    select * from tableA as A
    where exists(select * from tableB as B where A.id=B.id) 
    

    Even OPTION RECOMPILE doesn’t look like it would help actually. It still doesn’t give you the plan you would have got with a literal 0=0

    declare @var bit = 0
    
    select * from 
    master.dbo.spt_values  as A
    where
    1=
    (case when 0 = @var then 1
          when exists(select null from master.dbo.spt_values as B where A.number=B.number) 
          then 1
          else 0
    end)
    option(recompile)
    

    Plan http://img189.imageshack.us/img189/3977/executionplan.jpg

    select * from 
    master.dbo.spt_values  as A
    where
    1=
    (case when 0 = 0 then 1
          when exists(select null from master.dbo.spt_values as B where A.number=B.number) 
          then 1
          else 0
    end)
    

    Plan http://img193.imageshack.us/img193/3977/executionplan.jpg

    RE: Question in comments. Try the following with the “Include Actual Execution Plan” option enabled.

    declare @var bit = datepart(second,GETDATE())%2
    
    print @var
    
    if (@var = 0)
    select * from 
    master.dbo.spt_values  --8BA71BA5-3025-4967-A0C8-38B9FBEF8BAD
    else
    select * from 
    master.dbo.spt_values  as A --8BA71BA5-3025-4967-A0C8-38B9FBEF8BAD
    where exists(select null from master.dbo.spt_values as B where A.number=B.number) 
    

    Then try

    SELECT usecounts, cacheobjtype, objtype, text, query_plan
    FROM sys.dm_exec_cached_plans 
    CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
    CROSS APPLY sys.dm_exec_query_plan(plan_handle) 
    where text like '%8BA71BA5-3025-4967-A0C8-38B9FBEF8BAD%'
    

    The Compiled Plan will look like

    Plan http://img178.imageshack.us/img178/3977/executionplan.jpg

    The Actual Execution Plan will show only one path was actually executed though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following piece of code : <select> <option value=0>foo</option> <option value=1 selected=selected>bar</option> </select>
Consider the following piece of code. int var; cout << (long)&var; My doubt is
Consider the following piece of code. <html> <body> <script> var x = 5; //globally
Consider the following piece of code: <html><head></head> <body> <script type=text/javascript> var outside_scope = outside
Consider the following piece of code: case class User(id: Int, name: String) object User{
Consider the following code piece: ... int N,var; vector<int> nums; cin >> N; while
Consider the following piece of code #include<iostream> #include<string> class A { private: char name[10];
Consider the following piece of code - class MyThread extends Thread { private int
Consider the following piece of code: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void)
Consider the following piece of code: 1 typedef std::deque<int> mydeque_t; 2 mydeque_t mydeque; 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.