lzth.net
当前位置:首页 >> sql 递归 >>

sql 递归

sql 递归查询的方法: 方法一:T-SQL递归查询 with Dep as ( select Id,DeptCode,DeptName from Department where Id=1 union all select d.Id,d.DeptCode,d.DeptName from Dep inner join Department d on dep.Id = d.ParentDeptId ) select * ...

declare @b nvarchar(2) =1; with abc(id,b_PriorID,level)as ( select id,b_PriorID,0 level from #tt where b_PriorID =@b union all select a.id,a.b_PriorID,t.level+1 from #tt a inner join abc t on t.id=a.b_PriorID ) select * from ab...

在SQL SERVER 2000 中你可以先一些自定义函数,或一些存储过程,实现递归: select level,TypeName from ProductType t START WITH t.ParentID=0 CONNECT BY PRIOR t.ProductTypeID= t.ParentID;

给你一个例子,跟这个差不多: CREATE TABLE T(ID INT ,PID INT) INSERT INTO T SELECT 1,0 UNION ALL SELECT 2,0 UNION ALL SELECT 3,1 UNION ALL SELECT 4,1 UNION ALL SELECT 5,2 UNION ALL SELECT 6,4 UNION ALL SELECT 7,3 UNION ALL SELEC...

--测试数据with area(id,"name",f_id,leve) as ( select 1,'中国',0,1 union all select 2,'湖北',1,2 union all select 3,'武汉',2,3 union all select 4,'云贵',1,2 union all select 5,'云南',4,3 union all select 6,'贵阳',4,3 union all s...

是说要根据给定的CategoryID,查询出顶级父类的CategoryID?

我来测一下,等会上传结果 你还有一个表没用到。 WITH cte AS ( SELECT RegionID,RegionName,RegionPID FROM [tbRegionTree] WHERE regionPID='01' UNION ALL SELECT d.RegionID,d.RegionName,d.RegionPID FROM cte c inner JOIN [tbRegionTree]...

with tt(select 语句1 --初始化 union all select 语句2) select * from tt 在select 语句2中from的表里关联with后面的表名tt就形成递推了。

CREATE TABLE #test ( A char(1), B char(1))GOINSERT INTO #test VALUES('a', 'b');INSERT INTO #test VALUES('b', 'c');INSERT INTO #test VALUES('c', 'd');INSERT INTO #test VALUES('d', 'e');INSERT INTO #test VALUES('e', 'f');INSERT I...

//获取表中所有idprotected List getIdList(String sql, Object ... params) { return xxx;} //获取该menu下的所有子节点private List getMenuChildrenIds(long menuId) { String sql = "select menu_id from test_tb where p_id = ? "; return...

网站首页 | 网站地图
All rights reserved Powered by www.lzth.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com