SQL2005 Common Table Expressions(CET)即通用表表达式. SQLSERVER CET递归使用案例: 1.普通案例 表结构如下: ;WITH cet_depart AS ( SELECT depart_id, depart_name FROM dbo.Depart WHERE depart_id = '004' --入口 UNION ALL --递归时必要 SELECT d.depart_id, d.depart_n
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
表结构 CREATE TABLE city( id INT IDENTITY(1,1) PRIMARY KEY, NAME NVARCHAR(100), ParentID INT , Parents NVARCHAR(100)) 数据: 循环代码: goWITH CET_c AS ( SELECT id,name,parentID FROM city WHERE id=4 UNION ALL --循环步骤 --1.获取当前节点数据 --2.根据父parentID来关联 --3.关联不到循序结束