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
Mr or Mrs. 500
Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)

Group: General Forum Members
Points: 528 Visits: 375
Comments posted to this topic are about the item Get month names ordered using recursion
mhotek-836094
mhotek-836094
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 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
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

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

Thanks
davoscollective
davoscollective
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3231 Visits: 1008
A recursive CTE is also row by row.
Krtyknm
Krtyknm
Mr or Mrs. 500
Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)

Group: General Forum Members
Points: 528 Visits: 375
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
Mr or Mrs. 500
Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)

Group: General Forum Members
Points: 528 Visits: 375
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
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3231 Visits: 1008
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
Mr or Mrs. 500
Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)

Group: General Forum Members
Points: 528 Visits: 375
It is Interesting, Idea is to make the SQL script very simple and reusable. Thanks for your comments.
Krtyknm
Krtyknm
Mr or Mrs. 500
Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)Mr or Mrs. 500 (528 reputation)

Group: General Forum Members
Points: 528 Visits: 375
You are welcome..
abhishekgupta109
abhishekgupta109
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 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