Last active
March 10, 2019 12:19
-
-
Save ezhov-da/500fb8bea636a7694fbc37df731e5a83 to your computer and use it in GitHub Desktop.
sql common рекурсивное построение дерева
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[code:]sql[:code]with tree (id, fio, occm, parentId, lvl/*, fullPath*/) as | |
( | |
select | |
id | |
,fio | |
,occ | |
,isnull(parentId, 0) as parentId | |
,1 as lvl | |
--,cast(fio as varchar(8000)) as fullPath | |
from V_E_FN_EMPL_BY_LEVEL_COLUMN | |
where | |
parentId is null | |
UNION ALL | |
select | |
t2.id | |
,t2.fio | |
,t2.occ | |
,t2.parentId | |
,t1.lvl + 1 as lvl | |
--,cast(isnull(t1.fullPath + '/', '') + t2.fio as varchar(8000)) as fullPath | |
from tree t1 | |
join V_E_FN_EMPL_BY_LEVEL_COLUMN t2 on | |
t1.id = t2.parentId | |
) | |
select * from tree | |
[/code] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment