MSSQL递归查询实例
作者:互联网
/*
-- 示例数据
DROP TABLE test_data.dbo.t_releation;
CREATE TABLE test_data.dbo.t_releation(
row_id INT PRIMARY KEY IDENTITY(1,1), -- 行号
parent_id VARCHAR(90) NOT NULL, -- 父项编码
child_id VARCHAR(90) NOT NULL, -- 子项编码
child_desc VARCHAR(90)); -- 子项描述
INSERT INTO test_data.dbo.t_releation(parent_id, child_id, child_desc)
VALUES
('ttt', 'L000', '钟表'),
('L000', 'L001','指针'),
('L000', 'L002','电池'),
('L001', 'L003','时针'),
('L001', 'L004','分针');
*/
WITH rbom AS
(SELECT parent_id, child_id, child_desc FROM test_data.dbo.t_releation WHERE parent_id = 'L000'
UNION ALL
SELECT a.parent_id, a.child_id, a.child_desc FROM test_data.dbo.t_releation a INNER JOIN rbom b ON a.parent_id = b.child_id)
SELECT * FROM rbom GO;
查询结果
标签:递归,parent,id,实例,child,test,dbo,data,MSSQL 来源: https://www.cnblogs.com/mc-r/p/16269054.html