T-SQL CTE错误:锚点和递归部分之间的类型不匹配

时间:2010-08-06 22:01:08

标签: sql-server-2005 tsql recursion common-table-expression

当我尝试执行特定的递归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".

这是胡说八道。每个字段都明确地转换为VARCHAR(MAX)。 请帮我。我已经在这里和其他地方读过很多关于这个问题的答案,所有这些答案都明确地建议了这个问题。我已经这样做了,仍然得到错误。

此代码将重现错误:

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;

如果您发现它相关,SELECT left(@@VERSION, 70)会返回:

Microsoft SQL Server 2005 - 9.00.4053.00 (X64)   May 26 2009 14:13:01 

1 个答案:

答案 0 :(得分:10)

Will A对我原帖的评论找到了关键 - 整理。我发布的查询也适用于master数据库。

检查整理表明我走在正确的轨道上。

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)

有些疯狂的搜索后来,我有这个怪异的代码,

  1. 明确指定每列的排序规则,
  2. 成功执行,
  3. 返回预期结果
  4. 即便:

    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;
    

    在我的结果窗口中精美地坐着是预期的:

    data_list
    ------------------------------------------------
    tsql rage;einstein.x.m;another@e.c;123@123.321;