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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:21:34+00:00 2026-06-03T00:21:34+00:00

How do I make the following T-SQL statement legal? I can copy the subquery

  • 0

How do I make the following T-SQL statement legal? I can copy the subquery that sets @Type variable for every CASE option, but I’d rather execute the subquery only once. Is it possible?

SELECT
      @Type = (SELECT CustomerType FROM dbo.Customers WHERE CustomerId = (SELECT CustomerId FROM dbo.CustomerCategories WHERE CatId= @CatId)),
      CASE
         WHEN @Type = 'Consumer'THEN dbo.Products.FriendlyName
         WHEN @Type = 'Company' THEN dbo.Products.BusinessName
         WHEN @Type IS NULL THEN dbo.Products.FriendlyName
         WHEN @Type = '' THEN dbo.Products.FriendlyName
      END Product,
      ...
FROM
      Products
INNER JOIN
      Category
...

Edit: modified my example to be more concrete…have to run now…will be back tomorrow…sorry for signing off short but have to pick up kids 😀 will check back tomorrow. THX!!

Clarification: I can’t separate the two: in the subquery’s where-clasue, I need to refer to columns from tables that’re used in the main query’s join stmt. If I separate them, then @Type will lose relevance.

  • 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-03T00:21:37+00:00Added an answer on June 3, 2026 at 12:21 am

    Why not just separate it into two operations? What do you think you gain by trying to glom them into a single statement?

    SELECT @Type = (subquery);
    
    SELECT CASE WHEN @type = 'Consumer'...
    

    At the risk of sounding obtuse, do you really need the variable at all? Why not:

    SELECT CASE WHEN col_form_subquery = 'Consumer' THEN ...
      END Product
    FROM (<subquery>) AS x;
    

    With that form you’ll need to decide whether you want to assign values to variables or retrieve results.

    You can also pull multiple variables, e.g.

    SELECT @Col1 = Col1, @Col2 = Col2
      FROM (<subquery>) AS x;
    
    -- then refer to those variables in your other query:
    
    SELECT *, @Col1, @Col2 FROM dbo.Products WHERE Col1 = @Col2;
    

    But this is all conjecture, because you haven’t shared enough specifics.

    EDIT okay, now that we have a real query and can understand a bit better what you’re after, let’s see if we can write you a new version. I’ll assume that you were only trying to store the @Type variable so you can re-use it within the query, and that you weren’t trying to store a value there to use later (after this query).

    SELECT CASE 
          WHEN c.CustomerType = 'Company' THEN p.BusinessName
          WHEN COALESCE(c.CustomerType, '') IN ('Consumer', '') THEN p.FriendlyName
        END
        --, other columns
    FROM dbo.Products AS p
    INNER JOIN dbo.Category AS cat 
      ON p.CatId = cat.CatId
    INNER JOIN dbo.CustomerCategories AS ccat
      ON ccat.CatId = cat.CatId
    INNER JOIN dbo.Customers AS c
      ON c.CustomerId = ccat.CustomerId
    WHERE cat.CategoryId = @CatId;
    

    Some notes:

    • I’m not sure why you thought subqueries are the right way to approach this. Usually it is much better (and clearer to other developers) to build proper joins and let SQL Server optimize the query overall instead of trying to be smart and optimize individual subqueries largely independent of the main query. A proper join will help to eliminate rows up front that would otherwise, through the subqueries, potentially be materialized – only to be discarded. Trust SQL Server to do its job, and in this case its job is to perform a join across multiple tables.
    • The join to dbo.Category might not be needed if the SELECT doesn’t need to display the category name. If so then change the where clause and remove that join (join to CusomterCategories instead).
    • The second case can be changed to a simple ELSE if you’ve covered all the possible scenarios.
    • I made an assumption about the join between Products and Category (why is Category not plural like the others?). If this isn’t it please fill us in.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sql statement that I need to make quicker. There are
In sql, I can make an if statement like the following If MY_STATE in
I'm trying to make the following SQL server statement to work, but I get
In a comment on a previous question, someone said that the following sql statement
How can I make the following SQL a Linq query: SELECT * FROM ORDERS
PostgreSQL is about to make me punch small animals. I'm doing the following SQL
How do I make SQL SERVER 2005 to ignore the backslash in the following
I currently have a prepared statement in Java which uses the following SQL statement
I am trying to make SELECT statement for following situation and need help to
I am using multiple joins in a statement and Have tried to make linq-to-SQl

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.