SQL递归查询函数

--查询父单位的函数
CREATE Function GetParentUnit(@UnitId bigint@Level int
Returns @Parents Table([UnitId] int ,[UnitName] nvarchar(64),[ParentUnitId] int
As 
Begin
    
If @Level < 1   --如果参数@Level小于“1”,则得到所有父单位
        Begin        
            
Insert @Parents 
                
Select UnitId,UnitName,ParentUnitId 
                
From [T_Unit] 
                
Where [UnitId] = (Select [ParentUnitId] 
                
From [T_Unit] Where [UnitId]=@UnitId)        
            
            
While @@ROWCOUNT > 0
                
Begin                     
                    
Insert @Parents
                        
Select B.UnitId,B.UnitName,B.ParentUnitId 
                        
From @Parents A Inner Join [T_Unit] B 
                        
On A.[ParentUnitId]=B.[UnitId] 
                        
Where B.[ParentUnitId] Not In 
                        (
Select Distinct [ParentUnitId] From @Parents
                
End
        
End
    
Else
        
Begin
            
Set @level = @Level -1
            
            
Insert @Parents 
                
Select UnitId,UnitName,ParentUnitId 
                
From [T_Unit] 
                
Where [UnitId] = (Select [ParentUnitId] 
                
From [T_Unit] Where [UnitId]=@UnitId)            
            
            
While @@ROWCOUNT > 0 And @level > 0 
                
Begin 
                    
Set @level = @Level -1 
                    
Insert @Parents
                        
Select B.UnitId,B.UnitName,B.ParentUnitId 
                        
From @Parents A Inner Join [T_Unit] B 
                        
On A.[ParentUnitId]=B.[UnitId] 
                        
Where B.[ParentUnitId] Not In 
                        (
Select Distinct [ParentUnitId] From @Parents
                
End
        
End
Return 
End

--查询子单位的函数
CREATE Function GetChildUnit(@UnitId bigint@Level int
Returns @Child Table([UnitId] int ,[UnitName] varchar(64),[ParentUnitId] int)
As 
Begin 
    
If @Level < 1  --如果参数@Level小于“1”,则得到所有子单位
        Begin            
            
Insert @Child 
                
Select UnitId,UnitName,ParentUnitId 
                
From [T_Unit] Where [ParentUnitId] = @UnitId 
            
While @@ROWCOUNT > 0
                
Begin                     
                    
Insert @Child 
                        
Select B.UnitId,B.UnitName,B.ParentUnitId 
                        
From @Child A Inner Join [T_Unit] B 
                        
On A.[UnitId]=B.[ParentUnitId] 
                        
Where B.[ParentUnitId] Not In 
                        (
Select Distinct [ParentUnitId] From @Child
                
End 
        
End
    
Else
        
Begin
            
Set @level = @Level -1 
            
Insert @Child 
                
Select UnitId,UnitName,ParentUnitId 
                
From [T_Unit] Where [ParentUnitId]=@UnitId 
            
While @@ROWCOUNT > 0 And @level > 0 
                
Begin 
                    
Set @level = @Level -1 
                    
Insert @Child 
                        
Select B.UnitId,B.UnitName,B.ParentUnitId 
                        
From @Child A Inner Join [T_Unit] B 
                        
On A.[UnitId]=B.[ParentUnitId] 
                        
Where B.[ParentUnitId] Not In 
                        (
Select Distinct [ParentUnitId] From @Child
                
End 
        
End    
    
Return 
End

--查询
select * from GetParentUnit(3,2)
select * from GetChildUnit(1,2)

--删除函数
drop Function GetParentsUnit
drop Function GetChildUnit
posted on 2007-10-29 16:12  迷你软件  阅读(2320)  评论(0编辑  收藏  举报

本网站绝大部分资源来源于Internet,本站所有作品版权归原创作者所有!!如有以下内容:章节错误、非法内容、作者署名出错、版权疑问、作品内容有违相关法律等请及时与我联系. 我将在第一时间做出响应!本站所有文章观点不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。