我有一个Url表
UrlId Followedby
1 NULL
2 1
3 2
我想写一个sp,它将urlid作为参数并返回所有行。 GetAllUrls 3 它会返回上面的行。
没有光标可以上面吗?
答案 0 :(得分:2)
答案 1 :(得分:1)
使用递归CTE,它看起来像这样
declare @UrlId int = 3
;with C as
(
select U.UrlId,
U.Followedby
from Url as U
where U.UrlId = @UrlId
union all
select U.UrlId,
U.Followedby
from Url as U
inner join C
on U.UrlId = C.Followedby
)
select UrlId,
Followedby
from C