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