给定一个类别@categoryId,查询应该递归地导航到最上面的超类别,这已经完成。
现在我还想生成一个字符串,这将是该过程中所有 CategoryName 的串联。
DECLARE @CategoryId AS int = 217;
WITH Categories AS
(
SELECT ParentCategoryId, CategoryName, '' AS strCategory
FROM Category
WHERE CategoryId = @CategoryId
UNION ALL
SELECT c.ParentCategoryId, c.CategoryName,
(c.CategoryName + ': ' + cts.strCategory) AS strCategory
FROM Category AS c
JOIN Categories AS cts
ON c.CategoryId = cts.ParentCategoryId
)
SELECT TOP 1 CategoryName, LEN(CategoryName) AS strLength
FROM Categories
ORDER BY strLength DESC
使用上面的代码,我收到以下错误:
Types don't match between the anchor and the recursive part in column
"strCategory" of recursive query "Categories".
感谢您的帮助