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 805715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:07:30+00:00 2026-05-15T00:07:30+00:00

I want to execute select statement within CTE based on a codition. something like

  • 0

I want to execute select statement within CTE based on a codition. something like below

;with CTE_AorB
(
  if(condition)
    select * from table_A
   else
    select * from table_B
),
CTE_C as
(
   select * from CTE_AorB // processing is removed
)

But i get error on this. Is it possible to have if else within CTEs? If not is there a work around Or a better approach.

Thanks.

  • 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-15T00:07:31+00:00Added an answer on May 15, 2026 at 12:07 am

    try:

    ;with CTE_AorB
    (
        select * from table_A WHERE (condition true)
        union all
        select * from table_B WHERE NOT (condition true)
    ),
    CTE_C as
    (
       select * from CTE_AorB // processing is removed
    )
    

    the key with a dynamic search condition is to make sure an index is used, Here is a very comprehensive article on how to handle this topic:

    Dynamic Search Conditions in T-SQL by Erland Sommarskog

    it covers all the issues and methods of trying to write queries with multiple optional search conditions. This main thing you need to be concerned with is not the duplication of code, but the use of an index. If your query fails to use an index, it will preform poorly. There are several techniques that can be used, which may or may not allow an index to be used.

    here is the table of contents:

      Introduction
          The Case Study: Searching Orders
          The Northgale Database
       Dynamic SQL
          Introduction
          Using sp_executesql
          Using the CLR
          Using EXEC()
          When Caching Is Not Really What You Want
       Static SQL
          Introduction
          x = @x OR @x IS NULL
          Using IF statements
          Umachandar's Bag of Tricks
          Using Temp Tables
          x = @x AND @x IS NOT NULL
          Handling Complex Conditions
       Hybrid Solutions – Using both Static and Dynamic SQL
          Using Views
          Using Inline Table Functions
       Conclusion
       Feedback and Acknowledgements
       Revision History

    if you are on the proper version of SQL Server 2008, there is an additional technique that can be used, see: Dynamic Search Conditions in T-SQL Version for SQL 2008 (SP1 CU5 and later)

    If you are on that proper release of SQL Server 2008, you can just add OPTION (RECOMPILE) to the query and the local variable’s value at run time is used for the optimizations.

    Consider this, OPTION (RECOMPILE) will take this code (where no index can be used with this mess of ORs):

    WHERE
        (@search1 IS NULL or Column1=@Search1)
        AND (@search2 IS NULL or Column2=@Search2)
        AND (@search3 IS NULL or Column3=@Search3)
    

    and optimize it at run time to be (provided that only @Search2 was passed in with a value):

    WHERE
        Column2=@Search2
    

    and an index can be used (if you have one defined on Column2)

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

Sidebar

Related Questions

I want to execute a prepared statement like prepare stmt1 from 'select * from
I want to execute simple statement: SELECT * FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Text;Database=C:\Temp\;','SELECT * FROM [test.csv]') And
I want to execute one mysql statement where I have a select that is
I want to run a SELECT statement and I want execute a DELETE statement
What I want is when @AddressCode is null then simply execute SELECT * FROM
I execute a select to get the structure of a table. I want to
I have have this query that i want to execute. SELECT warehouse.expiry_date, pharmacy.expiry_date, drugs.active_substance,
I want to execute a program which takes in 1 text file(say like Text.exe),
I want to execute a code helloword.cpp which takes in some argument from console
I want to be able to get some output from mysql stored procedure within

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.