Guest User

Untitled

a guest
Aug 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. FOREACH Recursive SQL Statement
  2. Select all Id's where the parent Id is null (All root entries)
  3. Foreach (Id)
  4. GenerateControlNum(Id, CurrentCounterValue, CurrentCounterValue)
  5.  
  6. GenerateControlNum(Id, CurrentCounterValue, ParentCounterValue)
  7. Set Id's ControlNum to CurrentCounterValue
  8. Set Id's ParentControlNum to CurrentCounterValue
  9.  
  10. Increment CurrentCounterValue
  11.  
  12. Select All Id's where ParentId == Id (All my direct children)
  13. Foreach (ChildId)
  14. GenerateControlNum(ChildId, CurrentCounterValue, Id's ControlNum);
  15.  
  16. ID ParentId ControlNum ParentControlNum
  17. 8C821027-A6F9-E011-AB48-B499BAE13A62 756F981E-A6F9-E011-AB48-B499BAE13A62 0 NULL
  18. D7DB6033-A6F9-E011-AB48-B499BAE13A62 756F981E-A6F9-E011-AB48-B499BAE13A62 0 NULL
  19. D2E36033-A6F9-E011-AB48-B499BAE13A62 C9E36033-A6F9-E011-AB48-B499BAE13A62 0 NULL
  20. 8FE66033-A6F9-E011-AB48-B499BAE13A62 58E66033-A6F9-E011-AB48-B499BAE13A62 0 NULL
  21. 37EC6033-A6F9-E011-AB48-B499BAE13A62 2FEC6033-A6F9-E011-AB48-B499BAE13A62 0 NULL
  22. 41EC6033-A6F9-E011-AB48-B499BAE13A62 2FEC6033-A6F9-E011-AB48-B499BAE13A62 0 NULL
  23. DDED6033-A6F9-E011-AB48-B499BAE13A62 BCED6033-A6F9-E011-AB48-B499BAE13A62 0 NULL
  24. DC69981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  25. 166A981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  26. 4D6A981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  27. 856A981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  28. F56A981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  29. 2E6B981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  30. 666B981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  31. 9D6B981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 NULL
  32.  
  33. ID ParentId ControlNum ParentControlNum
  34. 8C821027-A6F9-E011-AB48-B499BAE13A62 756F981E-A6F9-E011-AB48-B499BAE13A62 22 21
  35. D7DB6033-A6F9-E011-AB48-B499BAE13A62 756F981E-A6F9-E011-AB48-B499BAE13A62 24 21
  36. D2E36033-A6F9-E011-AB48-B499BAE13A62 C9E36033-A6F9-E011-AB48-B499BAE13A62 58 57
  37. 8FE66033-A6F9-E011-AB48-B499BAE13A62 58E66033-A6F9-E011-AB48-B499BAE13A62 69 68
  38. 37EC6033-A6F9-E011-AB48-B499BAE13A62 2FEC6033-A6F9-E011-AB48-B499BAE13A62 86 85
  39. 41EC6033-A6F9-E011-AB48-B499BAE13A62 2FEC6033-A6F9-E011-AB48-B499BAE13A62 88 85
  40. DDED6033-A6F9-E011-AB48-B499BAE13A62 BCED6033-A6F9-E011-AB48-B499BAE13A62 95 94
  41. DC69981E-A6F9-E011-AB48-B499BAE13A62 NULL 0 0
  42. 166A981E-A6F9-E011-AB48-B499BAE13A62 NULL 1 1
  43. 4D6A981E-A6F9-E011-AB48-B499BAE13A62 NULL 2 2
  44. 856A981E-A6F9-E011-AB48-B499BAE13A62 NULL 3 3
  45. F56A981E-A6F9-E011-AB48-B499BAE13A62 NULL 4 4
  46. 2E6B981E-A6F9-E011-AB48-B499BAE13A62 NULL 5 5
  47. 666B981E-A6F9-E011-AB48-B499BAE13A62 NULL 6 6
  48. 9D6B981E-A6F9-E011-AB48-B499BAE13A62 NULL 7 7
  49.  
  50. 1
  51. 4
  52. 7
  53. 8
  54. 5
  55. 2
  56. 6
  57. 3
  58.  
  59. 1
  60. 2
  61. 3
  62. 4
  63. 5
  64. 6
  65. 7
  66. 8
  67.  
  68. ;with C as
  69. (
  70. select ID,
  71. ParentID,
  72. ControlNum,
  73. ParentControlNum,
  74. row_number() over(order by ParentID, ID) - 1 as rn
  75. from YourTable
  76. )
  77. update C1
  78. set ControlNum = C1.rn,
  79. ParentControlNum = case when C1.ParentID is null
  80. then C1.rn
  81. else C2.rn
  82. end
  83. from C as C1
  84. left outer join C as C2
  85. on C1.ParentID = C2.ID
  86.  
  87. ;with R as
  88. (
  89. select ID,
  90. ParentID,
  91. cast(ID as varchar(max)) as Sort
  92. from YourTable
  93. where ParentID is null
  94. union all
  95. select T.ID,
  96. T.ParentID,
  97. R.Sort+cast(T.ID as varchar(max))
  98. from YourTable as T
  99. inner join R
  100. on R.ID = T.ParentID
  101. ),
  102. C as
  103. (
  104. select ID,
  105. ParentID,
  106. row_number() over(order by Sort) - 1 as rn
  107. from R
  108. )
  109. update T
  110. set ControlNum = C1.rn,
  111. ParentControlNum = case when C1.ParentID is null
  112. then C1.rn
  113. else C2.rn
  114. end
  115. from YourTable as T
  116. inner join C as C1
  117. on T.ID = C1.ID
  118. left outer join C as C2
  119. on T.ParentID = C2.ID
Add Comment
Please, Sign In to add comment