sql server 中,查询指定子节点的所有父节点

create function f_pid(@id int) returns @t_level table(id int)  
as  
begin  
	insert into @t_level select @id 
	select @id = parent_id from dbo.media_folder where id = @id and id is not null  
while @@ROWCOUNT > 0  
	begin  
		insert into @t_level select @id select @id = parent_id from dbo.media_folder where id = @id and id is not null  
	end  
	return  
end  
go  

select * from f_pid(12) order by id

drop function f_pid

 

sql server 中,查询指定子节点的所有父节点笔记

你可能感兴趣的:(SQL Server)