sql语句关于递归 描述:根据主类别编号查询商品要求:主类别下的所有子类别及子类别下的所有子类别一次类推

2025-01-21 11:31:07
推荐回答(1个)
回答1:

create proc aa
@cateName varchar(200)
as
DECLARE @ID int

SET @ID =(select top 1 cate_id from xo_category_description where cate_name=@cateName)

;WITH T AS
(
SELECT xo_category.cate_id,xo_category.parent_id,xo_category.date_add
FROM xo_category
WHERE cate_id = @ID
UNION ALL
SELECT A.cate_id ,a.parent_id,a.date_add
FROM xo_category AS A JOIN T AS B ON A.parent_id = B.cate_id
)
SELECT * FROM T ORDER BY cate_id