Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 982991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:47:06+00:00 2026-05-16T04:47:06+00:00

I get the following error when I try to execute a particular recursive CTE:

  • 0

I get the following error when I try to execute a particular recursive CTE:

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

This is nonsense. Each field is explicitly cast to VARCHAR(MAX).
Please help me. I’ve read many answers to this problem, here and elsewhere, all of which advise explicitly casting the column in question. I’m already doing this, and still get the error.

This code will reproduce the error:

if object_id('tempdb..#tOwner') IS NOT NULL drop table #tOwner;
CREATE TABLE #tOwner(id int identity(1,1), email varchar(max) );
insert into #towner values ( cast('123@123.321'  as varchar(max)));
insert into #towner values ( cast('tsql rage'    as varchar(max)));
insert into #towner values ( cast('another@e.c'  as varchar(max)));
insert into #towner values ( cast('einstein.x.m' as varchar(max)));

;WITH data AS (
    SELECT DISTINCT convert(varchar(max), email) datapoint FROM #tOwner 
), CTE ( data_list, datapoint, length ) AS ( 
        SELECT convert(VARCHAR(max),            ''           ),convert(VARCHAR(max),    ''     ),       0
    UNION ALL
        SELECT convert(VARCHAR(max),d.datapoint+';'+data_list),convert(VARCHAR(max),d.datapoint), length + 1
        FROM CTE c CROSS JOIN data d WHERE d.datapoint > c.datapoint 
)
SELECT D.data_list
FROM ( 
    SELECT data_list, RANK() OVER ( PARTITION BY 1 ORDER BY length DESC ) 
    FROM CTE 
) D ( data_list, rank )
WHERE rank = 1 ;

drop table #tOwner;

If you find it relevant, SELECT left(@@VERSION, 70) returns:

Microsoft SQL Server 2005 - 9.00.4053.00 (X64)   May 26 2009 14:13:01 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:47:06+00:00Added an answer on May 16, 2026 at 4:47 am

    Will A‘s comment on my original post found the key – the collation. My posted query worked for me in the master database, too.

    Examining the collation suggested I was on the right track.

    SELECT DATABASEPROPERTYEX('crm_mscrm', 'Collation') crmSQLCollation
    crmSQLCollation
    --------------------
    Latin1_General_CI_AI
    (1 row(s) affected)
    
    SELECT DATABASEPROPERTYEX('master', 'Collation') masterSQLCollation
    masterSQLCollation
    ----------------------------
    SQL_Latin1_General_CP1_CI_AS
    (1 row(s) affected)
    

    Some frenzied searching later, I had this monstrosity of code, which

    1. explicitly specifies collation on each column,
    2. successfully executes, and
    3. returns the expected results

    To wit:

    if object_id('tempdb..#tOwner') IS NOT NULL drop table #tOwner;
    CREATE TABLE #tOwner(id int identity(1,1), email nvarchar(max) );
    insert into #towner values ( cast('123@123.321'  as nvarchar(max)));
    insert into #towner values ( cast('tsql rage'    as nvarchar(max)));
    insert into #towner values ( cast('another@e.c'  as nvarchar(max)));
    insert into #towner values ( cast('einstein.x.m' as nvarchar(max)));
    
    ;WITH data AS (
        SELECT DISTINCT convert(nvarchar(max), email) datapoint FROM #tOwner 
    ), CTE ( data_list, datapoint, length ) AS ( 
            SELECT convert(nvarchar(max),            ''           ) Collate SQL_Latin1_General_CP1_CI_AS,convert(nvarchar(max),    ''     ) Collate SQL_Latin1_General_CP1_CI_AS,       0
        UNION ALL
            SELECT convert(nvarchar(max),d.datapoint+';'+data_list) Collate SQL_Latin1_General_CP1_CI_AS,convert(nvarchar(max),d.datapoint) Collate SQL_Latin1_General_CP1_CI_AS, length + 1
            FROM CTE c CROSS JOIN data d WHERE d.datapoint > c.datapoint 
    )
    SELECT D.data_list
    FROM ( 
        SELECT data_list, RANK() OVER ( PARTITION BY 1 ORDER BY length DESC ) 
        FROM CTE 
    ) D ( data_list, rank )
    WHERE rank = 1 ;
    
    if object_id('tempdb..#tOwner') IS NOT NULL drop table #tOwner;
    

    Sitting beautifully in my results window is the expected:

    data_list
    ------------------------------------------------
    tsql rage;einstein.x.m;another@e.c;123@123.321;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error when I try and execute the code down below.
When I try to run this view/function, I get the following error execute() first
I get the following error when I try to execute this code segment :
I get following error message, when I try to run git rebase -i for
I get the following error when I try to run sample example of Google
I get the following error message when I try the following: Dim XL As
I get the following error on my Cloud Foundry installation when I try to
I get the following error in the UDK Frontend when I try to make
When I try to I get the following error in SQL Server Management Studio:
When I try to start an IntentService over adb i get tho following error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.