CTE代码只是抓住锚而不进行递归

时间:2017-09-14 09:25:08

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

查询没有进行递归,它只获取锚点部分(父id为null的id)。

任何人都可以帮助我发现我的错误吗?我尝试了很多东西,但我总是检索相同数量的行,没有父ID的IDS。

WITH EmpTree
AS (
    SELECT e.DWH_Dim_TFS_File_DWH_FileParent_Guid
        ,e.DWH_Dim_TFS_File_DWH_File_Guid
        ,1 AS Depth
        ,File_Name_String = CAST(CAST(e.File_Name AS BINARY (100)) AS VARBINARY(8000))
    FROM [dbo].[Hierarchy_Luis] AS e
    WHERE e.DWH_Dim_TFS_File_DWH_FileParent_Guid IS NULL

    UNION ALL

    SELECT e.DWH_Dim_TFS_File_DWH_FileParent_Guid
        ,e.DWH_Dim_TFS_File_DWH_File_Guid
        ,p.Depth + 1 AS Depth
        ,File_Name_String = CAST(CONCAT (
                p.File_Name_String
                ,CAST(e.File_Name AS BINARY (100))
                ) AS VARBINARY(8000))
    FROM [dbo].[Hierarchy_Luis] AS e
    JOIN EmpTree AS p ON e.DWH_Dim_TFS_File_DWH_FileParent_Guid = p.DWH_Dim_TFS_File_DWH_File_Guid
    )
SELECT p.DWH_Dim_TFS_File_DWH_File_Guid
    ,p.Depth
    ,Level01 = CAST(SUBSTRING(p.File_Name_String, 1, 100) AS NVARCHAR(100))
    ,Level02 = CAST(SUBSTRING(p.File_Name_String, 101, 100) AS NVARCHAR(100))
    ,Level03 = CAST(SUBSTRING(p.File_Name_String, 201, 100) AS NVARCHAR(100))
    ,Level04 = CAST(SUBSTRING(p.File_Name_String, 301, 100) AS NVARCHAR(100))
    ,Level05 = CAST(SUBSTRING(p.File_Name_String, 401, 100) AS NVARCHAR(100))
    ,Level07 = CAST(SUBSTRING(p.File_Name_String, 501, 100) AS NVARCHAR(100))
    ,Level08 = CAST(SUBSTRING(p.File_Name_String, 601, 100) AS NVARCHAR(100))
    ,Level09 = CAST(SUBSTRING(p.File_Name_String, 701, 100) AS NVARCHAR(100))
    ,Level10 = CAST(SUBSTRING(p.File_Name_String, 801, 100) AS NVARCHAR(100))
    ,Level11 = CAST(SUBSTRING(p.File_Name_String, 901, 100) AS NVARCHAR(100))
    ,Level12 = CAST(SUBSTRING(p.File_Name_String, 1001, 100) AS NVARCHAR(100))
    ,Level13 = CAST(SUBSTRING(p.File_Name_String, 1101, 100) AS NVARCHAR(100))
    ,Level14 = CAST(SUBSTRING(p.File_Name_String, 1201, 100) AS NVARCHAR(100))
    ,Level15 = CAST(SUBSTRING(p.File_Name_String, 1301, 100) AS NVARCHAR(100))
    ,Level16 = CAST(SUBSTRING(p.File_Name_String, 1401, 100) AS NVARCHAR(100))
    ,Level17 = CAST(SUBSTRING(p.File_Name_String, 1501, 100) AS NVARCHAR(100))
    ,Level18 = CAST(SUBSTRING(p.File_Name_String, 1601, 100) AS NVARCHAR(100))
    ,Level19 = CAST(SUBSTRING(p.File_Name_String, 1701, 100) AS NVARCHAR(100))
    ,Level20 = CAST(SUBSTRING(p.File_Name_String, 1801, 100) AS NVARCHAR(100))
    ,Level21 = CAST(SUBSTRING(p.File_Name_String, 1901, 100) AS NVARCHAR(100))
    ,Level22 = CAST(SUBSTRING(p.File_Name_String, 2001, 100) AS NVARCHAR(100))
    ,Level23 = CAST(SUBSTRING(p.File_Name_String, 2101, 100) AS NVARCHAR(100))
    ,Level24 = CAST(SUBSTRING(p.File_Name_String, 2201, 100) AS NVARCHAR(100))
    ,Level25 = CAST(SUBSTRING(p.File_Name_String, 2301, 100) AS NVARCHAR(100))
    ,Level26 = CAST(SUBSTRING(p.File_Name_String, 2401, 100) AS NVARCHAR(100))
    ,Level27 = CAST(SUBSTRING(p.File_Name_String, 2501, 100) AS NVARCHAR(100))
FROM EmpTree p

You can see in the link the two columns data types where the recursion is being made, it is a nvarchar(256)

非常感谢,我知道它必须是非常基本的东西,但我找不到。

示例数据

DWH_Dim_TFS_File_DWH_FileParent_GUID    DWH_Dim_TFS_File_DWH_File_GUID  File_Name
114087616   114087617   ot_de
114087616   114087618   ot_de
114087593   114087619   _proj
11407441    11408762    I_EXP
114087593   114087620   arm\
114087620   114087621   e\
114087621   114087622   ot_de
114087621   114087623   ot_de
114087593   114087624   c\
114087624   114087625   ot_de
114087624   114087626   ot_de
114087593   114087627   3s896
114087593   114087628   kefil
114087593   114087629   adme.
11408762    11408763    B_PTS
114087593   114087630   mdk\
114087630   114087631   ot_de
114087630   114087632   ot_de
114087593   114087633   urcer
114087633   114087634   ot_de
114087633   114087635   ot_de
114087593   114087636   artup
114087593   114087637   artup
114087593   114087638   artup
114087593   114087639   artup
11408763    11408764    B_PTS
114087593   114087640   artup
114087302   114087641   ot_et
114087641   114087642   _conf
114087641   114087643   ot_et
114087641   114087644   ot_et
114087641   114087645   ot_et
114087641   114087646   ot_et
114087641   114087647   ot_et
114087641   114087648   s\
114087648   114087649   csimp
11408763    11408765    B_PTS
114087648   114087650   cspro
114087648   114087651   dtbui
114087648   114087652   dtpro
114087648   114087653   rojec
114087648   114087654   ettin
114087654   114087655   g.ecl
114087648   114087656   cros.
114087648   114087657   rget_
114087641   114087658   dered
114087658   114087659   ot_et
11408763    11408766    cessW

1 个答案:

答案 0 :(得分:0)

这是基于前面评论的答案:

解决方案是他没有任何匹配,因为他们是空的 - 他必须在第一次选择时匹配它们。

WITH EmpTree
AS (
SELECT e.DWH_Dim_TFS_File_DWH_FileParent_Guid
    ,e.DWH_Dim_TFS_File_DWH_File_Guid
    ,1 AS Depth
    ,File_Name_String = CAST(CAST(e.File_Name AS BINARY (100)) AS VARBINARY(8000))

FROM [dbo].[Hierarchy_Luis] AS e WHERE e.DWH_Dim_TFS_File_DWH_FileParent_Guid = e.DWH_Dim_TFS_File_DWH_File_Guid

UNION ALL

SELECT e.DWH_Dim_TFS_File_DWH_FileParent_Guid
    ,e.DWH_Dim_TFS_File_DWH_File_Guid
    ,p.Depth + 1 AS Depth
    ,File_Name_String = CAST(CONCAT (
            p.File_Name_String
            ,CAST(e.File_Name AS BINARY (100))
            ) AS VARBINARY(8000))
FROM [dbo].[Hierarchy_Luis] AS e
JOIN EmpTree AS p ON e.DWH_Dim_TFS_File_DWH_FileParent_Guid = p.DWH_Dim_TFS_File_DWH_File_Guid
)
SELECT p.DWH_Dim_TFS_File_DWH_File_Guid
,p.Depth
,Level01 = CAST(SUBSTRING(p.File_Name_String, 1, 100) AS NVARCHAR(100))
,Level02 = CAST(SUBSTRING(p.File_Name_String, 101, 100) AS NVARCHAR(100))
,Level03 = CAST(SUBSTRING(p.File_Name_String, 201, 100) AS NVARCHAR(100))
,Level04 = CAST(SUBSTRING(p.File_Name_String, 301, 100) AS NVARCHAR(100))
,Level05 = CAST(SUBSTRING(p.File_Name_String, 401, 100) AS NVARCHAR(100))
,Level07 = CAST(SUBSTRING(p.File_Name_String, 501, 100) AS NVARCHAR(100))
,Level08 = CAST(SUBSTRING(p.File_Name_String, 601, 100) AS NVARCHAR(100))
,Level09 = CAST(SUBSTRING(p.File_Name_String, 701, 100) AS NVARCHAR(100))
,Level10 = CAST(SUBSTRING(p.File_Name_String, 801, 100) AS NVARCHAR(100))
,Level11 = CAST(SUBSTRING(p.File_Name_String, 901, 100) AS NVARCHAR(100))
,Level12 = CAST(SUBSTRING(p.File_Name_String, 1001, 100) AS NVARCHAR(100))
,Level13 = CAST(SUBSTRING(p.File_Name_String, 1101, 100) AS NVARCHAR(100))
,Level14 = CAST(SUBSTRING(p.File_Name_String, 1201, 100) AS NVARCHAR(100))
,Level15 = CAST(SUBSTRING(p.File_Name_String, 1301, 100) AS NVARCHAR(100))
,Level16 = CAST(SUBSTRING(p.File_Name_String, 1401, 100) AS NVARCHAR(100))
,Level17 = CAST(SUBSTRING(p.File_Name_String, 1501, 100) AS NVARCHAR(100))
,Level18 = CAST(SUBSTRING(p.File_Name_String, 1601, 100) AS NVARCHAR(100))
,Level19 = CAST(SUBSTRING(p.File_Name_String, 1701, 100) AS NVARCHAR(100))
,Level20 = CAST(SUBSTRING(p.File_Name_String, 1801, 100) AS NVARCHAR(100))
,Level21 = CAST(SUBSTRING(p.File_Name_String, 1901, 100) AS NVARCHAR(100))
,Level22 = CAST(SUBSTRING(p.File_Name_String, 2001, 100) AS NVARCHAR(100))
,Level23 = CAST(SUBSTRING(p.File_Name_String, 2101, 100) AS NVARCHAR(100))
,Level24 = CAST(SUBSTRING(p.File_Name_String, 2201, 100) AS NVARCHAR(100))
,Level25 = CAST(SUBSTRING(p.File_Name_String, 2301, 100) AS NVARCHAR(100))
,Level26 = CAST(SUBSTRING(p.File_Name_String, 2401, 100) AS NVARCHAR(100))
,Level27 = CAST(SUBSTRING(p.File_Name_String, 2501, 100) AS NVARCHAR(100))
FROM EmpTree p