Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
10,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. declare @typenamespace varchar(200)
  2. declare @everyone_ID int
  3. declare @origin varchar(200)
  4.  
  5. set @typenamespace = '14cb48ab-e10a-4a66-a992-e5201c3f1083.group'
  6. set @everyone_ID = (select id from TypeInstance where typeNamespace = @typenamespace and parentID = -1)
  7. set @origin = 'BARIUM_BBE_GROUPS'
  8.  
  9. -- Get BBE groups
  10. select id, parent_id, case when NoOfChildren = 0 then 0 else 1 end hasChildren, name, description into #groups from BariumRegistry..[Group]
  11.  
  12. -- Fill TypeInstance basic
  13. insert into TypeInstance (parentID, typeNamespace, securityID, keyValue, hasChildren, origin, originKey)
  14. select -1, @typenamespace, newid(), name, hasChildren, @origin, convert(varchar, id) from #groups
  15.  
  16. -- Update TypeInstance.parentId
  17. update child
  18. set parentID = case when g.id <> g.parent_id then parent.id else @everyone_ID end
  19. from
  20. TypeInstance child
  21. inner join #groups g on child.originKey = convert(varchar, g.id)
  22. inner join TypeInstance parent on convert(varchar, g.parent_id) = parent.originKey
  23. where
  24. child.origin = @origin and
  25. parent.origin = @origin
  26.  
  27. -- Fill TypeInstanceInfo
  28. insert into TypeInstanceInfo (typeInstanceId, cultureID, name, description)
  29. select ti.id, 'neutral', g.name, g.description
  30. from
  31. #groups g
  32. inner join TypeInstance ti on convert(varchar, g.id) = ti.originKey
  33. where
  34. ti.typeNamespace = @typeNamespace and
  35. ti.origin = @origin
  36.  
  37. -- Remove Properties without proper groups
  38. delete P
  39. from
  40. Property p
  41. left outer join #groups g on p.typeInstanceID = convert(varchar, g.id)
  42. where
  43. p.typeNamespace = @typeNamespace and
  44. g.id is null
  45.  
  46. -- Update Property.typeInstanceID
  47. update p
  48. set typeInstanceID = ti.ID
  49. from
  50. Property p
  51. inner join TypeInstance ti on p.typeInstanceID = ti.originKey
  52. where
  53. p.typeNamespace = @typeNamespace and
  54. ti.origin = @origin
  55.  
  56. -- Update TypeInstance.hierarchyIndex
  57. declare @lvl int
  58. set @lvl = 1
  59. select @everyone_ID id, @lvl lvl into #groupTree
  60.  
  61. while @@rowcount > 0
  62. begin
  63. set @lvl = @lvl + 1
  64.  
  65. update child
  66. set hierarchyIndex = parent.hierarchyIndex + ',' + convert(varchar, child.ID)
  67. from
  68. #groupTree g
  69. inner join TypeInstance parent on g.id = parent.ID
  70. inner join TypeInstance child on parent.ID = child.parentID
  71. where
  72. g.lvl = @lvl - 1 and
  73. (parent.origin = @origin or parent.ID = @everyone_ID) and
  74. child.origin = @origin
  75.  
  76. insert into #groupTree (id, lvl)
  77. select child.id, @lvl
  78. from
  79. #groupTree g
  80. inner join TypeInstance child on g.id = child.parentID
  81. where
  82. g.lvl = @lvl - 1 and
  83. child.origin = @origin
  84. end
  85.  
  86. --update TypeInstance set origin = null where origin = @origin
  87. drop table #groups
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement