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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:27:21+00:00 2026-06-18T10:27:21+00:00

Is there a way in SQL Server 2008 do to something like below? Can

  • 0

Is there a way in SQL Server 2008 do to something like below?

Can the outer applied(joined) table to be specified based on a condition?

declare @bGetExtendedInfo bit
set @bGetExtendedInfo = 1

declare @param nvarchar(24)
set @param = 'CO-02-BBB'

select t1.*, t2.ID
from t1
outer apply (
              case when @bGetExtendedInfo= 0 
                  then (select 0) as ID  /* dummy value */
                  /*really expensive query trying to avoid when extended info is not needed*/
                  else (select top 1 ID from tbl1 where tbl1.code = @param)
              end
             ) t2
  • 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-06-18T10:27:22+00:00Added an answer on June 18, 2026 at 10:27 am

    You can readily do this with just a join:

    SELECT t1.*, t2.*
    FROM   t1 cross join
           (SELECT *
            FROM (SELECT top 1 tb10.*, 0 as bCcond src FROM tb10 
                  UNION ALL
                  SELECT top 1 tb11.*, 1 as bCcond src FROM tb11
                 ) t
            WHERE @bCond = bCcond
           ) t2
    

    Doing the top before the union all should also help the optimizer produce a better query plan, in the event that the tables are really complex joins.

    Also, doing top without an order by is generally frowned upon. It can return different rows with different invocations, but the rows are not guaranteed to be random.

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

Sidebar

Related Questions

Using SQL Server 2008, is there a way to allow inserts to a table
I've a large (1TB) table in SQL Server 2008 that looks something like this:
Is there a way to do foreach value do something in sql server 2008.
IN SQL Server 2008 is there a way to generate a report as to
Is there any way to debug a SQL Server 2008 query?
Using Microsoft SQL Server 2005, is there any way to see when a table
I have a table in SQL Server 2008 that looks kind of like this:
In my SQL Server (2008 R2) on Azure, there's a table containing a lot
Is there a way in SQL Server to list all the views within a
is there some way to tell sql server to use the (nolock) hint or

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.