此CTE递归查询中的错误:锚点和递归部分之间的类型不匹配

时间:2019-05-13 21:20:29

标签: sql sql-server common-table-expression

我遇到此错误:

  

消息240,第16级,状态1,第7行
  递归查询“ Anchor”的列“ Calculated_pa​​rtnum”中的锚点与递归部分之间的类型不匹配

根据此处显示的代码:

with [Anchor] as 
(
    select 
        [Part].[Company] as [Part_Company],
        [Part].[PartNum] as [Part_PartNum],
        (1) as [Calculated_level],
        null as [Calculated_partnum]
    from 
        Part as Part
    inner join  
        PartMtl as PartMtl on Part.Company = PartMtl.Company
                           and Part.PartNum = PartMtl.PartNum

    union all

    select 
        [Anchor].[Part_Company],
        [Part1].[PartNum] as [Part1_PartNum],
        (Anchor.Calculated_Level + 1) as [Calculated_NLevel],
        [Anchor].[Part_PartNum] as [Part_PartNum]
    from   
        Anchor as Anchor
    inner join 
        PartMtl as PartMtl1 on Anchor.Part_Company = PartMtl1.Company
                            and Anchor.Part_PartNum = PartMtl1.PartNum
    inner join 
        Part as Part1 on Part1.Company = PartMtl1.Company
                      and Part1.PartNum = PartMtl1.MtlPartNum
)
select 
    [Anchor1].[Part_Company] as [Part_Company],
    [Anchor1].[Part_PartNum] as [Part_PartNum],
    [Anchor1].[Calculated_level] as [Calculated_level],
    [Anchor1].[Calculated_partnum] as [Calculated_partnum]
from  
    Anchor as Anchor1

0 个答案:

没有答案