Sean Lange (4/8/2014)
Eirikur Eiriksson (4/8/2014)
hisakimatama (4/8/2014)
Aha, so it doesn't :-). I've always declared CTEs with the column declarations, and by the wonderful problems of habit, it stuck as "the" way to do it. Well, I learned something that should make syntax a good bit clearer myself 😀I prefer it, makes the code more readable.
😎
That is just an opinion. There are times when it makes sense and times when it doesn't.
However, every column in a cte MUST be named. If you have a derived column of some sort if MUST have a name.
In the following you will see I have a column with the constant 'asdf' but the column has no name. This will not parse.
with MyCte as
(
select top 5 'asdf'
, name
from sys.objects
)
select * from MyCte;
But, simply add a column alias and it is fine.
with MyCte as
(
select top 5 'asdf' as MyColumn
, name
from sys.objects
)
select * from MyCte;
Thanks Sean, I should have been more clear on that.
😎