SQL 存储过程查找所有父ID

--查找父,包括自己
CREATE
PROC [dbo].[usp_spGetParentsID] @InputID INT --@ParentID UNIQUEIDENTIFIER OUTPUT AS BEGIN DECLARE @strResult Nvarchar; set @strResult=''; with cte as ( select ID,ParentID from Zitian_Categories where id=@InputID union all select a.id,a.ParentID from Zitian_Categories a join cte b on a.id=b.parentid ) select CONVERT(NVARCHAR,[id]) from cte --SELECT @strResult=CONVERT(NVARCHAR,[id])+','+@strResult from cte --set @strResult=substring(@strResult,1,(len(@strResult)-1)) --print @strResult END

 

posted @ 2014-01-25 11:28  zhangsir  阅读(181)  评论(0编辑  收藏  举报