--成语回环 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;