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
 General SQL Server Forums
 New to SQL Server Programming
 cte expression in sql server (;with or with)

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-09-10 : 16:34:27
I just recently came across the term CTE in sql server and since then I've been trying to learn more about it.

But one thing I observe that in some examples there is a semicolon before 'With' and in some examples there is no semicolon.


;with cte
as


or

with cte
as


Similarly, in some examples people have mentioned column names in the with clause itself as below but in some they don't.

with cte (column1, column2, column3)
as

or

with cte
as


I would like to know if they all mean the same or if they are based on any different situations.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-09-10 : 16:38:39
Because WITH is an overloaded keyword in SQL Server, a semicolon must precede it when declaring a CTE. Many people use ";WITH" just to be safe, but as long as any preceding statements are terminated with a semicolon, it's not strictly necessary.

The column list for a CTE is optional, just like it is for a view definition or an INSERT statement. Though it's a good practice to always specify column names.
Go to Top of Page
   

- Advertisement -