Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ezhov-da/500fb8bea636a7694fbc37df731e5a83 to your computer and use it in GitHub Desktop.
Save ezhov-da/500fb8bea636a7694fbc37df731e5a83 to your computer and use it in GitHub Desktop.
sql common рекурсивное построение дерева
[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