SQL Server遞迴查詢
create table test_with(
pid int,
parentid int,
name varchar(60)
)
insert into test_with values(1, null, 'A')
insert into test_with values(2, null, 'B')
insert into test_with values(3, null, 'C')
insert into test_with values(4, 1, 'A1')
insert into test_with values(5, 1, 'A2')
insert into test_with values(6, 1, 'A3')
update test_with set name = 'A2' where pid = 5
update test_with set name = 'A3' where pid = 6
select * from test_with
insert into test_with values(7, 4, 'A11')
insert into test_with values(8, 5, 'A22')
insert into test_with values(9, 6, 'A33')
with t(pid, parentid, name, level) as (
select pid, parentid, name, 0 as level from test_with where parentid is null
and pid = 1
union all
select test_with.pid, test_with.parentid, test_with.name, level+1 from t , test_with
where t.pid = test_with.parentid and test_with.parentid is not null
and t.pid <> test_with.pid
)
select * from t where level <= 3 OPTION(MAXRECURSION 5000)
with t(pid, parentid, name,parentname, level) as (
select pid, parentid, name, cast(null as varchar(60)) as parentname,0 as level from test_with where parentid is null
union all
select test_with.pid, test_with.parentid, test_with.name , t.name as parentname, level+1 from t , test_with
where t.pid = test_with.parentid
)
select * from t where level <= 3 OPTION(MAXRECURSION 5000)
可以用來模擬start with..connect by
在test_with.pid上糾結了許久,不小心寫成t.pid,結果造成無限的遞迴出錯:
訊息 530,級別 16,狀態 1,第 1 行
語句被終止。完成執行語句前已用完最大遞迴 5000。
這個特性在Oracle 11g, 12c也已支援
pid int,
parentid int,
name varchar(60)
)
insert into test_with values(1, null, 'A')
insert into test_with values(2, null, 'B')
insert into test_with values(3, null, 'C')
insert into test_with values(4, 1, 'A1')
insert into test_with values(5, 1, 'A2')
insert into test_with values(6, 1, 'A3')
update test_with set name = 'A2' where pid = 5
update test_with set name = 'A3' where pid = 6
select * from test_with
insert into test_with values(7, 4, 'A11')
insert into test_with values(8, 5, 'A22')
insert into test_with values(9, 6, 'A33')
with t(pid, parentid, name, level) as (
select pid, parentid, name, 0 as level from test_with where parentid is null
and pid = 1
union all
select test_with.pid, test_with.parentid, test_with.name, level+1 from t , test_with
where t.pid = test_with.parentid and test_with.parentid is not null
and t.pid <> test_with.pid
)
select * from t where level <= 3 OPTION(MAXRECURSION 5000)
with t(pid, parentid, name,parentname, level) as (
select pid, parentid, name, cast(null as varchar(60)) as parentname,0 as level from test_with where parentid is null
union all
select test_with.pid, test_with.parentid, test_with.name , t.name as parentname, level+1 from t , test_with
where t.pid = test_with.parentid
)
select * from t where level <= 3 OPTION(MAXRECURSION 5000)
可以用來模擬start with..connect by
在test_with.pid上糾結了許久,不小心寫成t.pid,結果造成無限的遞迴出錯:
訊息 530,級別 16,狀態 1,第 1 行
語句被終止。完成執行語句前已用完最大遞迴 5000。
這個特性在Oracle 11g, 12c也已支援
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8520577/viewspace-1062114/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql server遞迴SQLServer遞迴
- SQL server資料庫with as子句與遞迴查詢的實現SQLServer資料庫遞迴
- SqlServer遞迴查詢SQLServer遞迴
- PostgreSQL 遞迴查詢SQL遞迴
- sqlalchemy 遞迴查詢SQL遞迴
- 遞迴查詢子元素遞迴
- sql-server高階查詢SQLServer
- java-遞迴(檔案查詢)Java遞迴
- Sql Server 的引數化查詢SQLServer
- SQL Server 跨資料庫查詢SQLServer資料庫
- sql-server不相關子查詢SQLServer
- sql-server相關子查詢SQLServer
- SQL Server2005使用CTE實現遞迴QCSQLServer遞迴
- 遞迴-M--二分查詢遞迴
- 遞迴樹形查詢所有分類遞迴
- SQL Server 查詢超時問題排查SQLServer
- SQL Server 語句日期格式查詢方法SQLServer
- SQL Server-簡單查詢語句SQLServer
- SQL Server查詢慢的解決方案SQLServer
- SQL Server 查詢歷史執行的SQL語句SQLServer
- iOS 演算法之排序、查詢、遞迴iOS演算法排序遞迴
- 資料庫遞迴查詢:MySQL VS Sequelize資料庫遞迴MySql
- 透過遞迴查詢應用依賴遞迴
- Java實現遞迴查詢樹結構Java遞迴
- SQL 遞迴思想SQL遞迴
- SQL SERVER死鎖查詢,死鎖分析,解鎖,查詢佔用SQLServer
- SQL Server 查詢表註釋和欄位SQLServer
- 第二章 :查詢與排序---------遞迴、查詢與排序補充排序遞迴
- 簡單C#遞迴(向前查詢上工序)C#遞迴
- 第二章 :查詢與排序-------二分查詢的遞迴解法排序遞迴
- 查詢SQL Server的歷史執行記錄SQLServer
- 使用SSMS連線和查詢 SQL Server 例項SSMSQLServer
- SQL server根據表名查詢表主鍵SQLServer
- DbForge Studio for SQL Server入門教程:在查詢生成器中建立查詢SQLServer
- 二分法查詢(遞迴實現)遞迴
- 記錄一次遞迴查詢的運用遞迴
- SQL Server中Table字典資料的查詢SQL示例程式碼SQLServer
- SQL Server資料庫————模糊查詢和聚合函式SQLServer資料庫函式
- SQL server資料庫表碎片比例查詢語句SQLServer資料庫