November 8, 2011 at 8:41 am
goofbauer (11/8/2011)
I now have a new favorite. :blush: The Itzik-style cross join. Begs the question, why does MS place the emphasis they do on cursors and rCTEs in certification. A bit like giving loaded guns to children, don't you think?
Ha! Perhaps. But recursive CTEs have their uses, and so too do cursors (though good uses of cursors are the rarer of the two). There are very few absolutes in the SQL Server world, so a good part of being 'good' at SQL is knowing which tool to use in which situation, and why...
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
November 8, 2011 at 2:57 pm
Thanks for the great ideas!
I think I'll try creating the tally table on the fly approach. I'm afraid my process might not be able to access master in production, so the spt_values my not work for me.
.
November 8, 2011 at 3:06 pm
BSavoie (11/8/2011)
I'm afraid my process might not be able to access master in production, so the spt_values my not work for me.
Frankly with these approaches, you can use any table that is sure to have more records than you need. I think it's a matter of being selective.
The alternative is the approach mentioned in the article SQL Kiwi linked above:
http://www.sqlservercentral.com/articles/T-SQL/74118/
Here's the snippet from that article:
WITH
E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), -- 1*10^1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), -- 1*10^2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), -- 1*10^4 or 10,000 rows
E8(N) AS (SELECT 1 FROM E4 a, E4 b) -- 1*10^8 or 100,000,000 rows
SELECT TOP (4000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E8
;
November 8, 2011 at 3:21 pm
Just for another example, you could also read this article: http://www.sqlservercentral.com/articles/T-SQL/67899/
November 8, 2011 at 5:15 pm
goofbauer (11/8/2011)
I now have a new favorite. :blush: The Itzek-style cross join. Begs the question, why does MS place the emphasis they do on cursors and rCTEs in certification. A bit like giving loaded guns to children, don't you think?
BWAAA-HAAAA!!! It's because they don't know what a Tally Table is. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
November 9, 2011 at 11:32 am
Yes Jeff, you are exactly right! That is the problem for me.
.
Viewing 6 posts - 16 through 20 (of 20 total)
You must be logged in to reply to this topic. Login to reply