Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server - user CTE in subquery

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?

like image 736
Frank Millman Avatar asked May 24 '11 08:05

Frank Millman


People also ask

What is faster CTE or subquery?

As for your question. The performance of CTEs and subqueries should, in theory, be the same since both provide the same information to the query optimizer. One difference is that a CTE used more than once could be easily identified and calculated once. The results could then be stored and read multiple times.

What is the difference between CTE and subquery?

A Common Table Expression (aka CTE, aka WITH statement) is a temporary data set to be used as part of a query. It only exists during the execution of that query; it cannot be used in other queries even within the same session (from Wikipedia). A subquery is a nested query; it's a query within a query (more Wikipedia).

Can we use two CTE in a single SELECT query?

After learning common table expressions or CTEs, a natural question is “Can I use several CTEs in one query?” Yes, you can!

Can CTE be nested?

Nested CTEs Common Table Expressions can be also nested. This means having multiple CTEs in the same query where at least one CTE refers to another CTE.


2 Answers

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.

like image 135
Andomar Avatar answered Oct 07 '22 06:10

Andomar


SQL Server does not support this much-required feature. I too have been looking for help on this. MS SQL Server does not support Temporary Views either as opposed to PostgreSQL. The above-mentioned solution is also likely to work only if all the CTE definitions could be generated before-hand and do not have conflicting names in each of the sub-queries either - the purpose being that these CTE definitions may be different for each level of a sub-query.

Sad but true !!!

Regards, Kapil

like image 44
Kapil Avatar answered Oct 07 '22 06:10

Kapil