sql查父節點小筆記

//因爲是取根節點下第一層所對0做了一個判斷 這裏自已能夠根據自已要的數據來取
ALTER
function [dbo].[f_pid](@id varchar(60)) returns @t_level table(pid varchar(60),sortNum int) as BEGIN DECLARE @sortNum int SET @sortNum = 0 --獲得當前id的父id, select @id = ParentId, @sortNum =Sortnum from Sys_Departments where keyId = @id and ParentId is not NULL while @@ROWCOUNT > 0 BEGIN --循環輸入插入 insert into @t_level select @id, @sortNum -------------------------------------------- --繼續找父id select @id = ParentId, @sortNum = Sortnum from Sys_Departments where keyId = @id and ParentId is not NULL --------------------------------------- IF @id<>'0' BEGIN DELETE @t_level --刪除 END ELSE BEGIN --只取當前根節點的下一個最高父節點 BREAK END end return end
相關文章
相關標籤/搜索