ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[F_GetChildCate]') and xtype in (N'FN', N'IF', N'TF')) dropfunction[dbo].[F_GetChildCate] GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO CreateFunction F_GetChildCate(@Idint) Returns@ChildTable([CateId]int,CateName nvarchar(127),ParentId int) As Begin Begin Insert@ChildSelect Id,C_CateName,C_ParentID From[T_Cate]Where[C_ParentID]=@Id While@@ROWCOUNT>0 Begin Insert@ChildSelect B.Id,B.C_CateName,B.C_ParentID From@Child A InnerJoin[T_Cate] B On A.[CateId]=B.[C_ParentID] Where B.[C_ParentID]NotIn (SelectDistinct[ParentId]From@Child) End End Return End GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO