sql 树 递归 with SubQuery(No,Name,ParentNo) as ( ' union all select A.No,A.Name,A.ParentNo from [Port_Dept] A inner join SubQuery B on A.No = B.ParentNo ) select * from SubQuery…
1.由人员计算出总数,在部门树(tree)按结构汇总(主父绑定) CREATE function [dbo].[GetEmpDepNum] ( @ID int ) RETURNS @Tree Table (ID [, ),PID Int,FID Int,SN Varchar(), Name Varchar(), Num Varchar()) as begin declare @MaxNum int,@i int,@f int,@sNnm int Insert @Tree SELECT c1.pi…
有理数的树,根节点是1/1,左儿子是1/2,右儿子是2/1....求给定的分数是第几个,或者给定n求第n个分数.递归.给定的分数,每次递归,如果分子比较小,就用分母减去分子,并且这是左儿子.反之是右儿子,终点是分子分母相等.求第n个,每次递归,如果n是奇数(为右儿子),新的分子是分子加分母.终点是n==1即到树根了,分子分母为1. #include<iostream> #define ll unsigned long long using namespace std; ll n,p,q,ans…
1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设OrganiseUnit(组织机构表)中主要的三个字段为OrganiseUnitID(组织机构主键ID).ParentOrganiseUnitID(组织机构父ID).OrganiseName(组织机构名称) with organise as (select * from OrganiseUnit where OrganiseUnit.OrganiseUnitID…
SQL Server 没有类似于Oracle START WITH NAME='xx' CONNECT BY PRIOR ID=PARENT_ID这样的语句,但是可以通过自定义标准函数+With语句实现,速度也是杠杠的 ALTER FUNCTION [dbo].[RecursionSysLocation] ( -- Add the parameters for the function here ) ) RETURNS TABLE AS RETURN ( with temp ( [Id], [p…
表结构是这样的 部门 上级部门 A BB CC DA AB BC C 求一条SQL语句,根据A查其上级部门,查询结果为上级部门BCD ================================================= 用函数create table tb (部门 varchar(20),上级部门 varchar(20)) insert into tb…
每个地区递归层级可能不一致,数据表(table)存放最小层级地区 area --地区层级表 id name f_id leve 1 中国 0 1 2 湖北 1 2 3 武汉 2 3 ... --测试数据 with area(id,"name",f_id,leve) as ( ,, union all ,, union all ,, union all ,, union all ,, union all ,, union all ,, union all ,, union all ,,…
Server 2005中提供了公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性,同时,CTE要比表变量的效率高得多. 存储过程方法: create proc up_delete_nclass @did int as with my1 as( select * from News_Class where id = @did union all select News_Class.* from my1, News_Class where my1.id = News_Class.Pare…
declare @startDay smalldatetime ='2013-01-01' ;with cte as( select @startDay as d union all select DATEADD(d,1,d) as d from cte where d<'2019-05-01') select * from cte --设置循环次数,0为无限制OPTION(MAXRECURSION 0)…
with temp as ( select Id, UserId, OfficeID, RoleId, DeptId, IsDelete, IsEnd, ParentId from [dbo].[DiGui_Demo] where UserId =1 or OfficeID = 56 or DeptId = 77 union all select a.Id, a.UserId, a.OfficeID, a.RoleId, a.DeptId, a.IsDelete, a.IsE…
alter function Fn_GetUserGroupRelation ( @DHsItemID int ) returns nvarchar(1024) begin declare @Col_HsItemID int declare @Result nvarchar(1024) set @Result='' select @Col_HsItemID = Col_HsItemID from Hs_Relation where Col_DHsItemID=@DHsItemID if @Col…
WITH cte_name AS ( --Anchor member is defined ' UNION ALL --Recursive member is defined referencing cte_name select a.PCY_Name,a.PCY_ID,a.PCY_Parent, a.PCY_Code,a.PCY_Status from PB_Code_MaterType a inner join cte_name c on a.PCY_Parent=c.PCY_ID ) --…
using System; using System.Collections.Generic; using System.Linq; namespace ConsoleAppTest { class Program { static void Main(string[] args) { var aa = new AA(); , Name = " }; aa.CreateTree(ref tree); Console.WriteLine(); } } public class Tree { pub…
with cte as ( select belongsAgent from [QPProxyDB].[dbo].[BS_ProxyInfo] where ProxyID = @ProxyID union all select a.ProxyID from [QPProxyDB].[dbo].[BS_ProxyInfo] a join cte b on a.ProxyID = b.belongsAgent ) select * from cte order by belongsAgent asc…
一.查询当前部门下的所有子部门 WITH dept AS ( SELECT * FROM dbo.deptTab --部门表 WHERE pid = @id UNION ALL SELECT d.* FROM dbo.deptTab d INNER JOIN dept ON d.pid = dept.id ) SELECT * FROM dept 二.查询当前部门所有上级部门 WITH tab AS ( SELECT DepId , ParentId , DepName , [Enable] ,…
)) INSERT INTO @t SELECT 'AAA,BBB,CCC' SELECT * FROM @t ;WITH mycte AS ( ,mend,num FROM @t UNION ALL ,num FROM mycte WHERE mend<=LEN(sentence) ) ),,,'') sentence FROM mycte 如果由excel中直接复制过来的,可能在没一个项前会有换行符,替换方法为 replace char(13) + char(10) 更简单的可以转换为xm…