Sql 递归

USE RJSite

GO

WITH temp

AS ( SELECT *

FROM dbo.VideoType

WHERE ID = 1 --要查询的子类ID

UNION ALL

SELECT v.*

FROM VideoType AS v ,

temp

WHERE v.PID = temp.ID -- 查询所有子类

)

SELECT *

FROM temp;

WITH temp2

AS ( SELECT *

FROM dbo.VideoType

WHERE ID = 9 --要查询的子类ID

UNION ALL

SELECT v.*

FROM VideoType AS v ,

temp2

WHERE v.ID = temp2.PID -- 查询所有父类

)

SELECT *

FROM temp2

posted @   爱笑的3  阅读(184)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示