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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:51:32+00:00 2026-05-22T15:51:32+00:00

This question has been asked before – How we can use CTE in subquery

  • 0

This question has been asked before –

How we can use CTE in subquery in sql server?

The only answer suggested was “Just define your CTE on top and access it in the subquery?”

This works, but I would really like to be able to use a CTE in the following scenarios –

  1. as a subquery in a SELECT

  2. as a derived table in the FROM clause of a SELECT

Both of these work in PostgreSQL. With Sql Server 2005, I get “Incorrect syntax near the keyword ‘with'”.

The reason I would like it is that most of my queries are constructed dynamically, and I would like to be able to define a CTE, save it somewhere, and then drop it in to a more complex query on demand.

If Sql Server simply does not support this usage, I will have to accept it, but I have not read anything that states that it is not allowed.

Does anyone know if it is possible to get this to work?

  • 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-22T15:51:33+00:00Added an answer on May 22, 2026 at 3:51 pm

    In SQL Server, CTE’s must be at the top of the query. If you construct queries dynamically, you could store a list of CTE’s in addition to the query. Before you send the query to SQL server, you can prefix the query with a list of CTE’s:

    ; with Cte1 as (...definition 1...),
      Cte2 as (...definition 2...),
      Cte3 as (...definition 3...),
      ...
    ...constructed query...
    

    This is assuming that you’re constructing the SQL outside of SQL Server.

    You could also consider creating views. Views can contain CTE’s, and they can be used as a subquery or derived table. Views are a good choice if you generate SQL infrequently, say only during an installation or as part of a deployment.

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

Sidebar

Related Questions

This question has been asked before, mainly with reference to ListActivity, and therefore I
This question has been asked before for other languages: Python , PHP and JavaScript
This question has been asked before (see here and here , for example). However,
This question has been asked before, but the answered ones I could find were
Undoubtedly this question has been asked before, except I completely lack the knowledge to
I'm sorry if this question has been asked before, but I'm not even sure
It feels like this question has been asked before, but no questions seem to
I am sorry if this question has been asked before (I tried to find
Folks, I know this question has been asked before here, though indirectly . But
Anyone knows how to create one? I know this question has been asked before

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.