views:

44

answers:

1

I recently learned about Recursive Common Table Expressions (CTE's) while looking for a way to build a certain view of some data. After taking a while to write out how the first iteration of my query would work, I turned it into a CTE to watch the whole thing play out. I was surprised to see that grouping didn't work, so I just replaced it with a "Select TOP 1, ORDER BY" equivalent. I was again surprised that "TOP" wasn't allowed, and came to find that all of these clauses aren't allowed in the recursive part of a CTE:

  • DISTINCT
  • GROUP BY
  • HAVING
  • TOP
  • LEFT
  • RIGHT
  • OUTER JOIN

So I suppose I have 2 questions:

  1. In order to better understand my situation and SQL, why aren't these clauses allowed?
  2. If I need to do some sort of recursion using some of these clauses, is my only alternative to write a recursive stored procedure?

Thanks.

+1  A: 

Referring to :-

1. In order to better understand my situation and SQL, why aren't these clauses allowed?

Based on my understanding of CTE's, the whole idea behind creating a CTE is so that you can create a temporary result-set and use it in a named manner like a regular table in SELECT, INSERT, UPDATE, or DELETE statements.

Because a CTE is logically very much like a view, the SELECT statement in your CTE query must follow the same requirements as those used for creating a view. Refer CTE_query_definition section in following link on MSDN

Also, because a CTE is basically a named resultset and it can be used like any table in SELECT, INSERT, UPDATE, or DELETE statements, you always have the option of using the various operators you mentioned when you use the CTE in any of those statements.

With regards to

2.If I need to do some sort of recursion using some of these clauses, 
is my only alternative to write a recursive stored procedure?

Also, i am pretty sure that you can use atleast some of the keywords that you have mentioned above in a view / CTE select statement. For Eg: Refer the use of Group By in a CTE here in the Creating a simple common table expression example

Maybe if you can provide a sample scenario of what you are trying to achieve, maybe we can suggest some possible solution

InSane
Both links refer to the same webpage and I would not recommend it since it contains a lot of inconsistencies and bugs. Have you really run scripts from it? and read it? Use http://msdn.microsoft.com/en-us/library/ms175972(v=SQL.100).aspx instead
vgv8
@vgv8 - Yes both links are to the same webpage - I was referring to the same page just different sections in my answer. Also, i was just asking the OP to see the example listed there for reference as to how to use the Group By. I am not aware of any inconsistencies there for the parts which i am referenced but if you can highlight those, that will help!!
InSane
In you link the script in section "C. Referencing a common table expression more than one time" does not reference CTE more than once. The following scripts do not run at all since engage nonexistent dbo.MyEmployees
vgv8
@vgv8 - Actually, i was referring only to the example "Creating a simple common table expression" as specified in my reply as that is the one using a GROUP BY clause as the OP had posted the Group By cannot be used in a CTE. But your point noted!!
InSane