Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 sql random number generator

Author  Topic 

anjana
Starting Member

1 Post

Posted - 2014-11-02 : 14:15:16
;WITH n(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM n WHERE n<36)
SELECT LEFT((SELECT ''+SUBSTRING('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',n,1)
FROM n ORDER BY NEWID() FOR XML PATH('')),10)


What does ;WITH n(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM n WHERE n<36) mean or do?

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-11-02 : 14:58:12
That defines a common table expression, which is like a subquery. In this case it is a recursive cte, since the right half of the union references the CTE name(n). FWIW that's an expensive way to generate a list of integers.

There is a larger discussion on this whole topic (called tally tables ) here: http://www.sqlservercentral.com/articles/Tally+Table/72993/
Go to Top of Page
   

- Advertisement -