Quantcast
Channel: Active questions tagged cte - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 207

Why should a CTE start with a semi-colon?

$
0
0

I was just looking at a post on StackOverflow where Aaron Bertrand proposes using a CTE instead of a numbers table, which is an elegant way of performing the task at hand. My question is, why does the first line of the CTE begin with a semi-colon?

;WITH n AS (SELECT TOP (10000) n FROM   (SELECT n = ROW_NUMBER() OVER    (ORDER BY s1.[object_id])    FROM sys.all_objects AS s1    CROSS JOIN sys.all_objects AS s2  ) AS x ORDER BY n)SELECT n FROM n ORDER BY n; -- look ma, no gaps!

Is this to ensure the WITH statement does not get parsed into a previous SELECT or something? I see nothing in SQL Server 2005 BOL about using a semi-colon prior to the WITH.


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>