## The Dynamic Tally or Numbers Table

 Author Message Lynn Pettis SSC-Insane Group: General Forum Members Points: 24167 Visits: 37929 spaghettidba SSCertifiable Group: General Forum Members Points: 5730 Visits: 13305 Excellent article, Lynn!I'll make sure to include your oustanding UDF in my dev/production environment. I already have one that does the same thing, in a while loop . Thanks for this content, it will be very useful! --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba Matt Whitfield SSC-Addicted Group: General Forum Members Points: 497 Visits: 719 Top job Lynn.It's this sort of article that really keeps me on my toes with respect to the SQL I write - there's almost always a better way, and this article has shown me a better way.Really good explanation too. Thanks! :-D Atlantis Interactive - SQL Server ToolsMy blogWhy I wrote a sql query analyzer clone peter-757102 SSC-Addicted Group: General Forum Members Points: 449 Visits: 2549 I quickly read the article and ran no tests myself, but if all this code is only used to quickly fill a table with numbers then I am perplexed about the complexity as well as the amount of code. We all have seen so many 5 line SQL code solutions that do the same, be it only for tally table generation.If the code is to dynamically generate a list of numbers in a desired range for immediate use in another statement I still find the code rather complex and would like to see comparisons with dozens of other tally solutions that can be found on this site. We all know by now that recursion is not the tool to use for this and to compare it with that doesn’t say much to me.Don't take this the wrong way, but can you clarify to me what the exact intended use is and if you did compare it with similar solutions that are not rooted in recursion? I can see the range and order aspect as something novel compared to pure tally functions, but a function that simply reads a tally table could do the same in a single statment by using some basic math.What required the many staged approach you took? Rob Fisk SSC-Enthusiastic Group: General Forum Members Points: 177 Visits: 428 I agree. Definitely an interesting article but I would have liked to see tests against an already created and indexed tally table and the reasons for going dynamic rather than static since it is quite easy to sort and scale results to any interval/order from a static table.I'll have a look myself of course but for now cannot thing of a reason to use a function over a static tally table. Maybe I'm missing something though. _______________________________________________________Change is inevitable... Except from a vending machine. peter-757102 SSC-Addicted Group: General Forum Members Points: 449 Visits: 2549 Here is an exampe of generating a range of numbers form an existing tally table or function with numbers starting at 1.`-- Get a range of numbers based on a tally table, the range can be 20bits wide-- (1048576 positions) in this example.declare @min bigint, @max bigint;select @min = 1000000000, @max = 1000000200;select top (@max - @min + 1) (@min - 1) + N from dbo.tally20b;`Even with a 12 bit tally table you can generate ranges of 4096 numbers at any position in the bigint range. Creating intervals is no more then adding a multiplyer (see below):`-- Get a range of numbers based on a tally table, the range can be 20bits wide-- (1048576 positions) in this example. An interval of 10 is used.declare @min bigint, @max bigint;select @min = 1000000000, @max = 1000000200;select top (@max - @min + 1) @min + (10 * (N - 1)) from dbo.tally20b;` Luke L SSCrazy Group: General Forum Members Points: 2680 Visits: 6103 Rob Fisk (9/22/2009)I agree. Definitely an interesting article but I would have liked to see tests against an already created and indexed tally table and the reasons for going dynamic rather than static since it is quite easy to sort and scale results to any interval/order from a static table.I'll have a look myself of course but for now cannot thing of a reason to use a function over a static tally table. Maybe I'm missing something though.I can't tell you the number of times a tally or numbers table approach has been suggested to someone on the forums here and the response was something to the affect of "I'm not allowed to change the schema" or "I can't get a static table like that approved." Seems like a quick way to dynamically generate a tally table of just the numbers you need would be very helpful in those circumstances...Good thorough description of the code Lynn, and a good read.Thanks,-Luke. To help us help you read thisFor better help with performance problems please read this Rob Fisk SSC-Enthusiastic Group: General Forum Members Points: 177 Visits: 428 Indeed, a very well written, descriptive and, as I said, interesting article. It's already been bookmarked so I can look at the interaction of the various elements in the code later since it seems they could help me with other things I have been looking at.I had completely forgotten the scenario of being able to create functions but not tables. Especially if those tables have no impact on the rest of the schema. I must have been on some happy juice and forgotten what a crazy world wee live in. _______________________________________________________Change is inevitable... Except from a vending machine. dbishop SSC Rookie Group: General Forum Members Points: 47 Visits: 248 I am a bit perplexed by the article. I buiul a tally table in each of my databases a couple of years ago. Static table, 100K rows, PK. When I initially built it I really did not care if it took 150ms or 1.3 seconds, because it was a one-time thing. A tally table is so useful, it should be part of the SQL installation and be included when a new database is created.An article on how to use a tally table would have been a lot better than how to create one. Likle it was said, there are a lot of articles that provide a 5-line code solution on how to create it all over the place. No reason to be creating one on the fly every time you need it. John Rowan SSCarpal Tunnel Group: General Forum Members Points: 4072 Visits: 4510 Great job Lynn! John Rowan ============================================================================================================ Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden

## 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
 SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 14 - Administration      SQL Server 14 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Red Gate Software         Quest Software         ApexSQL         Lumigent         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12