CTE error: "Types don't match between the anchor and the recursive part"

incorrect syntax cte
sql cte
multiple cte sql
cte semicolon
incorrect syntax near the keyword 'with' cte
always begin cte with a semicolon
expecting recompile or result

I am executing the following statement:

;WITH cte AS (
  SELECT 
    1 as rn, 
    'name1' as nm
  UNION ALL
  SELECT 
    rn + 1,
    nm = 'name' + CAST((rn + 1) as varchar(255))
  FROM cte a WHERE rn < 10)
SELECT * 
FROM cte

...which finishes with the error...

Msg 240, Level 16, State 1, Line 2
Types don't match between the anchor and the recursive part in column "nm" of recursive query "cte".

Where am I making the mistake?


Exactly what it says:

'name1' has a different data type to 'name' + CAST((rn+1) as varchar(255))

Try this (untested)

;with cte as
(
select 1 as rn, CAST('name1' as varchar(259)) as nm
union all
select rn+1,nm = 'name' + CAST((rn+1) as varchar(255))
from cte a where rn<10)
select * from cte

Basically, you have to ensure the length matches too. For the recursive bit, you may have to use CAST('name' AS varchar(4)) if it fails again

CTE Error: Incorrect syntax near the keyword 'with'. If this statement , I am having this error when using execute query for CTE. Help will be appriciated​. Thursday, August 3, 2006 12:23 PM. Reply. |. Quote. CTE Error: Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, SQL Server >


You need to cast both nm fields

;with cte as
(
select  1 as rn, 
        CAST('name1' AS VARCHAR(255)) as nm
union all
select  rn+1,
        nm = CAST('name' + CAST((rn+1) as varchar(255)) AS VARCHAR(255))
from cte a where rn<10)
select * from cte

Why should a CTE start with a semi-colon?, Your code broke! I got this error message: Incorrect syntax near 'WITH' While I'd like to believe that folks  I used SQL Server and a query that uses a CTE. It gives me an error "Incorrent syntax near the Keyword 'WITH'.."if this is a common table expressionprevious statement must be terminated witha semicolon." The query runs fine outside of Power BI. I tried putting a semi-colon before the 'WITH', but that didn't work.


For me problem was in different collation.

Only this helped me:

;WITH cte AS (
  SELECT 
    1 AS rn, 
    CAST('name1' AS NVARCHAR(4000)) COLLATE DATABASE_DEFAULT AS nm
  UNION ALL
  SELECT 
    rn + 1,
    nm = CAST('name' + CAST((rn + 1) AS NVARCHAR(255)) AS NVARCHAR(4000)) COLLATE DATABASE_DEFAULT
  FROM cte a WHERE rn < 10)
SELECT * 
FROM cte;

Hope it can help someone else.

Common Table Expression (CTE) getting an error, Hi, i'm converting my CTE codes from SQL server to Denodo/VQL but i'm getting an error it say "QUERY VIRTUAL ERROR". I have a multiple  I'm using a CTE to convert SSRS stored proc's into BO stored proc's, as apparently I can't use temp tables with Business Objects. I'm having this query: ;WITH cte1 AS ( SELECT cv.issue


;with cte as
(
select 1 as rn, 'name' + CAST(1 as varchar(255)) as nm
union all
select rn+1,nm = 'name' + CAST((rn+1) as varchar(255))
from cte a where rn<10)
select * from cte

Solved: Error by using CTE (WITH), Solved: Earlier this week I started to having problems by using CTE on DirectQuery, as the error below. I didnt identify any syntax erron on the. 1) Create direct query dataset with a CTE - let it error, then hit Edit Queries. 2) COPY/PASTE the dataset with the CTE, and Edit the query in the new one so that it selects out the same columns as the CTE result set, only '' AS [Column1], '' AS [Column2], etc


In my case, I messed up the sequence of columns in top and bottom clauses of UNION ALL. And it turned out that a varchar column appeared 'under' an int one. An easy mistake to make of you have lots of columns

Solved: Custom query with CTE not supported (bug?), When use With CTE clause, Power BI Desktop will treat this clause as a derived table, then cause syntax error. We can use SQL Profiler to trace executed  When executing a CTE, any hints that reference a CTE may conflict with other hints that are discovered when the CTE accesses its underlying tables, in the same manner as hints that reference views in queries. When this occurs, the query returns an error. Guidelines for Defining and Using Recursive Common Table Expressions


Error Occurs When CTE Alias is 'RESULT', Error occurs when CTE Alias is 'RESULT' - Incorrect syntax near 'RESULT'. Succeeds on some SQL Server databases but fails on others. I came across the issue with CTE at the final "Apply" stage of Power Query with a Direct Connection and while the preview was working, the Apply and Close would fail with the same message as OP's. Using JimJao's fix with Ole DB I got it to Apply and Update.


Football's CTE problem is real—divulging the NFL's dismissal of the , CTE is a degenerative brain disease linked to professions with repetitive head trauma—athletes, veterans, and fighters [1]. Tau proteins,  A common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement, possibly multiple times.


Invalid object name SQL Server CTE Error, This error is faced when a select query uses more than one time a CTE : Invalid object name. (1 row(s) affected) Msg 208, Level 16, State 1, Line  A Common Table Expression, also called as CTE in short form, is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. The CTE can also be used in a View. In this article, we will see in detail about how to create and use CTEs from our SQL Server. Syntax and Examples for Common Table Expressions