ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

sql server 部门递归函数

2022-04-30 17:01:14  阅读:153  来源: 互联网

标签:递归函数 -- Manager server isnull deptcode DeleteFlag sql nvarchar


create function [dbo].[ft_get_recursion_deptlist](  
@deptcode nvarchar(20),--部门代码  
@direction bit =0 --递归方向 0表示向上,1表示向下 
--@showDelete bit =0--0表示不显示删除部门,1表示显示删除部门
) 

returns @result table(DeptName nvarchar(50),DeptCode nvarchar(30),ParentDeptCode nvarchar(30),DeptCategoryCode nvarchar(20),
  DeptCategory nvarchar(50),id int ,CostCode nvarchar(30) , ProfitCode  nvarchar(30),managerEmpCode nvarchar(10),managerJobCode nvarchar(20),
  managerPostCode nvarchar(20),deptpath nvarchar(500),IsProdDept bit ,IsOrderProdDept bit,IsIndependent varchar(1),
  BUCode nvarchar(30),Companyid nvarchar(10),DeleteFlag varchar(1))
as  

  begin  
    if(@direction=0)  
    begin   

      --向上递归,获取指定部门向上的所有部门  
      WITH deptlist   
      AS  
      (  
          SELECT a.deptName,a.deptcode,a.parentdeptcode,a.DeptCategoryCode,a.DeptCategory,a.id, a.CostCode,a.ProfitCode ,
          a.Manager_EmployeeCode,a.Manager_JobCode,a.Manager_PositionCode, cast(a.deptcode as nvarchar(4000)) AS PATH,  
          a.IsProdDept ,a.IsOrderProdDept,isnull(a.IsIndependent,'N') as IsIndependent,
          case when a.DeptCategoryCode='BU' then a.deptcode else isnull((select bucode from ft_get_bucode(a.deptcode)),a.Companyid) end as BUCode ,
          a.Companyid,isnull(a.DeleteFlag ,'N') as DeleteFlag
          FROM sys_company_dept a  
          WHERE a.deptcode = @deptcode  and  isnull(a.deleteflag,'N')!='Y'

          UNION ALL  

          SELECT b.deptName,b.deptcode,b.parentdeptcode,b.DeptCategoryCode,b.DeptCategory,b.id, b.CostCode,b.ProfitCode ,
          b.Manager_EmployeeCode,b.Manager_JobCode,b.Manager_PositionCode,d.PATH+'->'+Cast(b.deptcode as nvarchar(4000)) PATH ,   
          b.IsProdDept ,b.IsOrderProdDept,isnull(b.IsIndependent,'N') as IsIndependent,
          case when b.DeptCategoryCode='BU' then b.deptcode else isnull((select bucode from ft_get_bucode(b.deptcode)),d.Companyid) end as BUCode,
          b.Companyid,isnull(b.DeleteFlag ,'N') as DeleteFlag
          FROM sys_company_dept b  
          INNER JOIN deptlist d ON d.parentdeptcode=b.deptcode  and isnull(b.deleteflag,'N')!='Y'--向上递归  
      )  

      insert into @result  
      SELECT * FROM deptlist  where isnull(deleteflag,'N')!='Y'

      --限制递归次数  
      OPTION(MAXRECURSION 8)  

 end  
 else  
 begin  
     --向下递归,获取指定部门向下的所有部门
      WITH deptlist   
      AS  
      (  
          SELECT a.deptName,a.deptcode,a.parentdeptcode,a.DeptCategoryCode,a.DeptCategory,a.id, a.CostCode,a.ProfitCode ,  
          a.Manager_EmployeeCode,a.Manager_JobCode,a.Manager_PositionCode,Cast(a.deptcode as nvarchar(4000)) AS PATH,  
          a.IsProdDept ,a.IsOrderProdDept,isnull(a.IsIndependent,'N') as IsIndependent,
          case when a.DeptCategoryCode='BU' then a.deptcode else isnull((select bucode from ft_get_bucode(a.deptcode)),a.Companyid) end as BUCode,
          a.Companyid,isnull(a.DeleteFlag ,'N') as DeleteFlag 
          FROM sys_company_dept a  
          WHERE a.deptcode = @deptcode  and isnull(a.deleteflag,'N')!='Y'

          UNION ALL  

          SELECT b.deptName,b.deptcode,b.parentdeptcode,b.DeptCategoryCode,b.DeptCategory,b.id, b.CostCode,b.ProfitCode ,  
          b.Manager_EmployeeCode,b.Manager_JobCode,b.Manager_PositionCode,d.PATH+'->'+Cast(b.deptcode as nvarchar(4000)) PATH,  
          b.IsProdDept ,b.IsOrderProdDept,isnull(b.IsIndependent,'N') as IsIndependent, 
          case when b.DeptCategoryCode='BU' then b.deptcode else isnull((select bucode from ft_get_bucode(b.deptcode)),b.Companyid) end as BUCode,
          b.Companyid ,isnull(b.DeleteFlag ,'N') as DeleteFlag
          FROM sys_company_dept b  
          INNER JOIN deptlist d ON d.deptcode=b.parentdeptcode and isnull(b.deleteflag,'N')!='Y'--向下递归  
      )  
      insert into @result  
      SELECT * FROM deptlist where isnull(deleteflag,'N')!='Y'

      --限制递归次数  
      OPTION(MAXRECURSION 8)  
 end  

 return;  

end

 

标签:递归函数,--,Manager,server,isnull,deptcode,DeleteFlag,sql,nvarchar
来源: https://www.cnblogs.com/gzb1/p/16210626.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有