很多时候我们会在数据库表中存储树结构的数据,如菜单:一级菜单.二级菜单.三级菜单... 如果树结构层次比较多,如何能够在只知道某节点的情况下,找到此节点下的所有子级数据呢? 在.NET后台可以定义一个递归函数,通过递归可以找到相应的数据. 那么在SQL中,用脚本如何递归查找呢? 在SQL2008以上版本有一个 WITH CTE AS 的用法,可以实现相应的业务.(只能使用一个with,多个CTE中间用逗号分隔) 例: -- 定义一个temp的"临时表" with temp as ( -
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.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 ) --
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