SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get month names ordered using recursion


Get month names ordered using recursion

Author
Message
Krtyknm
Krtyknm
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 374
Comments posted to this topic are about the item Get month names ordered using recursion
mhotek-836094
mhotek-836094
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 61
Ummmm. Recursive CTE, why?

Single SELECT statement based on AdventureWorks:

SELECT DISTINCT DATENAME(mm,OrderDate), MONTH(OrderDate)
FROM Sales.SalesOrderHeader
WHERE OrderDate BETWEEN '1/1/2006' and '12/31/2006'
ORDER BY MONTH(OrderDate)

Michael Hotek
President - Champion Valley Software, Inc.
http://www.ChampionValleySoftware.com
Hardy21
Hardy21
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1560 Visits: 1399
Thanks for sharing. Easy to use.

Thanks
davoscollective
davoscollective
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 1004
A recursive CTE is also row by row.
Krtyknm
Krtyknm
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 374
Good Question!! This CTE does not depends on any tables. Think of the environment that do not have any tables which has the date for the consecutive month and you need to accomplish the requirements.In this case you cannot depend on any tables. Also keep performance and re-usability in mind.

Thanks,
Karthik
Krtyknm
Krtyknm
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 374
Yes, recursive CTE is also row by row. When you are not joined any tables then recursive CTE will restrict upto 100 rows.
davoscollective
davoscollective
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 1004
The server wide maxrecursion default is 100, irrespective of whether you join to a table or not.
BOL: http://msdn.microsoft.com/en-us/library/ms175972.aspx

More dangerous is that "if the recursive member query definition returns the same values for both the parent and child columns, an infinite loop is created" effectively the same as setting maxrecursion to 0, so good practice would be to specify the option for maxrecursion.

Anyway, what you are doing is tiny - only 12 rows so none of this matters.
Unless it ends up in a correlated subquery which of itself would be bad.

If you are looking at something reusable then you may as well have a table with the monthnames and numbers, assuming the bizarre situation that you have a database with no dates in it. Given this is a paremeter for a report, then logically there must be dates in the data for it to be usable.

In most SSRS parameter situations, it's even more robust to source the options from the data itself.

Thanks for the article, it prompted me to read the rCTE BOL entry. :-)
Krtyknm
Krtyknm
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 374
It is Interesting, Idea is to make the SQL script very simple and reusable. Thanks for your comments.
Krtyknm
Krtyknm
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 374
You are welcome..
abhishekgupta109
abhishekgupta109
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 26
Interesting one....

but why would you display a month name, if we do not have any data for it. (just for the sake of displaying a blank report.)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search