SHARE
TWEET

Untitled

a guest Jul 21st, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table chiefTree(
  2. id int  NOT NULL
  3. ,parent_id  int NULL
  4. ,name       varchar(255) NOT NULL
  5. )
  6.  
  7. insert value chiefTree
  8. (id,parent_id,name) primary key
  9. values(1,null,'Начальник1')
  10. ,(1,1,'Начальник2')
  11. ,(1,2,'Начальник3')
  12. ,(1,3,'Начальник4')
  13. ,(1,4,'Начальник5')
  14.  
  15. create users(
  16. id  int identity(1,1) NOT NULL
  17. ,parent_id int NOT NULL REFERENCES chiefTree (id)
  18. ,name   varchar(255) not null
  19. )
  20.      
  21. select
  22.     t1.name as [Уровень1]
  23.     ,t2.name as [Уровень2]
  24.     ,t2.name as [Уровень3]
  25.     ,t3.name as [Уровень4]
  26.     ,t4.name as [Уровень5]
  27. from chiefTree t1
  28. left join chiefTree t2  on t2.id= t1.parent_id
  29. left join chiefTree t3  on t3.id= t2.parent_id
  30. left join chiefTree t4  on t4.id= t3.parent_id
  31. left join chiefTree t5  on t5.id= t4.parent_id
  32. where t1.parent_id = null
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top