• RV
    • Missy – My Home
    • JBAM – Newmar Dutch Star
    • Shaneeda
    • Buying a Used RV
    • Gas vs Diesel RVs
    • Ideal Motorhome
    • Choosing the Ideal RV
  • Cycling
  • Jeep
  • About
  • Flying
    • Vans RV-4
    • Daisy – RV-3B
  • Resources
  • Subscribe

JdFinley.com

Fulltime RV Living Adventures

  • Is Solar For You?
  • Gas vs Diesel RVs
  • Buying a Used RV
  • Choosing the Ideal RV
  • Ideal Motorhome

SQL Common Table Expressions

August 13, 2011

Microsoft SQL Server 2000 2005 2008 T-SQL Tips How-To Development Programming

I’ve been working with Microsoft SQL Server Common Table Expressions (CTE) lately and am totally impressed every time.

My most recent experience was needing a temporary table with the fixed number of rows into which I would load some data based on the row number (basically).  There are several ways to accomplish this with T-SQL.  The most common is a loop such as this:

DECLARE @Test TABLE (Counter INT)
DECLARE @Count INT
SET @Count = 0

WHILE (@Count < 100)
BEGIN
  SET @Count = @Count + 1
   
  INSERT @Test (Counter)
  VALUES (@Count)

END
 
SELECT * FROM @Test 

The above works but is far from elegant.  Using a Common Table Expression, the same thing can be accomplished with this:

WITH cte AS
(SELECT 1 AS count
  UNION ALL
 SELECT count + 1
   FROM cte
  WHERE count < 100
)
SELECT count
  FROM cte OPTION (MAXRECURSION 100)

You can use this query to populate a table by modifying the above to Insert the results into a table variable, temporary table, or a table.  This examples uses a table variable.

DECLARE @Test TABLE (Counter INT);

WITH cte AS
(SELECT 1 AS count
  UNION ALL
 SELECT count + 1
   FROM cte
  WHERE count < 100
)
INSERT @Test (Counter)
SELECT count
  FROM cte OPTION (MAXRECURSION 100)

From a performance perspective:
Loop count of 30: While loop requires 0 milliseconds. The CTE requires 0 milliseconds.
Loop count of 10,000: While loop requires 403 milliseconds. The CTE requires 167 /milliseconds
Loop count of 32,767: While loop requires 790 milliseconds. The CTE requires 540 milliseconds.
Loop count of 500,000: While loop requires 9.34 seconds. The CTE requires 8.097 seconds.

My conclusion is that on smallish counts (< 5000) the performance is about the same. On large to very large counts (> 5000 AND < 500,000) the Common Table Expressions approach is faster.  On very large counts, While and Common Table Expressions are again pretty similar.  I have no idea why this is but I did find it interesting.  I suspect my ability to record performance and machine resources may have a lot to do with it so your mileage may vary based on your environment.

Common Table Expressions are very powerful, very cool, and here to stay – learn to take advantage of them!

(Visited 154 times, 1 visits today)

Related posts:

Default ThumbnailWhat is SQL? Default ThumbnailBuffer Size

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Life is a lemon and I want my money back.

— Meatloaf

Popular Posts

  • Powermatic Model 90 Wood Lathe & VFD
  • Wood Turning on a Lathe
  • Chainsaw Review: Sportsman 20 in. 52cc
  • Cielo Grande Barbed Wire Fence Repair

Email Notification


 

Posts by Subject

3DPrinting Aircraft Airstrip Bible Bus bus boys Camping Cat Computers Cycling Development Electrical electronics Entertainment Environment Factory Five family FAQ Finances flying Food God Health Holiday Home Humor kayak Life Maintenance Nature Photography Review RV-3 RV-4 sailboat sailing self improvement Sewing solar Travel Video weather wood woodworking X-Country

Posts by Month

© Copyright © 2025 JDFinley.com · All Rights Reserved · Privacy Policy

Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to JD Finley and JdFinley.com with appropriate and specific direction to the original content.