sql server 部门递归函数
2022/4/30 19:12:46
本文主要是介绍sql server 部门递归函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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
这篇关于sql server 部门递归函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)