Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Help with CTE syntax error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lars Zeb - 08 Jul 2008 20:52 GMT
Can anyone help me with "incorrect syntax near 'cte'"?

Thanks in advance, Lars

with cte
as
(
select
  AddressID, PersonID, DateAdded,
  row_number() over(partition by AddressID order by PersonID DESC) as
rn
from
  dbo.Person
)
select * cte
where rn > 1;

Table definition:

create table Person (
  AddressID int,
  PersonID int,
  DateAdded datetime)
Aaron Bertrand [SQL Server MVP] - 08 Jul 2008 20:59 GMT
> Can anyone help me with "incorrect syntax near 'cte'"?
>
> with cte

CTEs need to be at the very beginning of the statement, because of the way
the "WITH" keyword has been overloaded.

So, just make sure you have a semi-colon at the end of the preceding
statement.  e.g. instead of:

SET NOCOUNT ON

WITH cte AS
(
   ...

Use:

SET NOCOUNT ON;

WITH cte AS
(
   ...

I have seen some use the following convention, but I am not particularly
fond of it (I have no objective evidence to back that up, either):

;WITH cte AS
(
   ...
Roy Harvey (SQL Server MVP) - 08 Jul 2008 21:13 GMT
The FROM in the outer SELECT is missing.

with cte
as
(
select
  AddressID, PersonID, DateAdded,
  row_number() over(partition by AddressID order by PersonID DESC) as
rn
from
  dbo.Person
)
select * FROM cte
where rn > 1;

Roy Harvey
Beacon Falls, CT

>with cte
>as
[quoted text clipped - 8 lines]
>select * cte
>where rn > 1;
Aaron Bertrand [SQL Server MVP] - 08 Jul 2008 21:19 GMT
Of course.

If my guess was right, the error message would have been slightly
different...

A
Roy Harvey (SQL Server MVP) - 08 Jul 2008 21:25 GMT
>Of course.
>
>If my guess was right, the error message would have been slightly
>different...

And if the post was typed in, instead of pasted, the problem I found
might not be the real problem at all, and your's could be the actual
answer.  It is certainly easily overlooked, as I prove to myself all
too often.

Roy
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.