成语接龙-成语回环-从自己开始又回到自己

--成语回环
SET STATISTICS TIME ON 
SET STATISTICS IO ON 
GO 
WITH cte_a
AS (SELECT [成语],
           CAST([成语] AS NVARCHAR(MAX)) AS cpath,
           [FirstWord],
           [LastWord],
           1 AS ilevel,
           0 AS isrecycle
    FROM dbo.cyzd
    WHERE 成语 = '国将不国'
    UNION ALL
    SELECT s.成语,
           CAST(p.cpath + '/' + s.[成语] AS NVARCHAR(MAX)) AS cpath,
           s.[FirstWord],
           s.[LastWord],
           p.ilevel + 1 AS ilevel,
           CASE
               WHEN s.成语='国将不国' THEN
                   1
               ELSE
                   0
           END AS isrecycle
    FROM dbo.cyzd AS s
        INNER JOIN cte_a AS p
            ON p.lastword = s.firstword
    WHERE p.ilevel + 1 <= 10
          AND CHARINDEX(s.成语, p.cpath) IN ( 0, 1 )
          AND p.isrecycle = 0)
SELECT *
FROM cte_a
WHERE cte_a.isrecycle = 1;

 

posted on 2019-08-21 11:34  hold_on_up  阅读(216)  评论(0编辑  收藏  举报