views:

10

answers:

1

I am learning CTE, and tried out the following query

WITH fooCTE
AS 
(
SELECT TOP 5 f.bar FROM foobar f
)

But it's displaying an error which is quite incomprehensible.

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.

Infact when I run the query below, I get the top five values being displayed.

SELECT TOP 5 f.bar FROM foobar f
96.8
92.4
99.3
68.9
74.8

SELECT f.bar from foobar f;
96.8
92.4
99.3
68.9
74.8
69.3
94.6
74.8
36.4
92.6
69.4
99.2
39.9
96.2
99.1

I hope am not wrong with the syntax and stuff. Where am I wrong?

+1  A: 

You defined fooCTE but you do nothing with it. try:

WITH fooCTE
AS 
(
SELECT TOP 5 f.bar FROM foobar f
)
select * from fooCTE
Ovidiu Pacurar
Thank you :) and I have another doubt the CTE is only valid for that session right?
Chaitanya
the CTE is valid for the current statement only.
Ovidiu Pacurar