Advertisement
JanuszKowalski123

10. hierarch 10 ok

May 9th, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. USE [2024_MZ202_ZSBD_7237]
  2. GO
  3. DROP TABLE [dbo].[Renewable_Energy_Devices];
  4.  
  5.  
  6. CREATE TABLE Renewable_Energy_Devices (
  7. device_id NVARCHAR(26) PRIMARY KEY,
  8. device_status INT CHECK (device_status >= 0 AND device_status <= 6),
  9. device_power DECIMAL(18,0),
  10. company_id INT,
  11. device_location GEOGRAPHY,
  12. device_location_geom GEOMETRY,
  13. device_hierarchy HIERARCHYID,
  14. device_description XML
  15. );
  16.  
  17.  
  18. DECLARE @latitude DECIMAL(9, 6), @longitude DECIMAL(9, 6);
  19. DECLARE @point GEOMETRY;
  20. DECLARE @device_hierarchy HIERARCHYID;
  21. DECLARE @device_description XML;
  22.  
  23. DECLARE @i INT = 1;
  24. WHILE @i <= 50
  25. BEGIN
  26. SET @latitude = RAND() * 180 - 90;
  27. SET @longitude = RAND() * 360 - 180;
  28. SET @point = GEOMETRY::Point(@latitude, @longitude, 4326);
  29.  
  30. -- Tworzenie hierarchii z użyciem losowej wartości dodanej do korzenia
  31. SET @device_hierarchy = HIERARCHYID::GetRoot().GetDescendant(NULL, NULL);
  32.  
  33. SET @device_description = '<description>Device_' + CAST(@i AS NVARCHAR(5)) + '</description>';
  34.  
  35. INSERT INTO Renewable_Energy_Devices (device_id, device_status, device_power, company_id, device_location, device_location_geom, device_hierarchy, device_description)
  36. VALUES (
  37. 'Device_' + CAST(@i AS NVARCHAR(5)),
  38. CAST(RAND() * 6 AS INT),
  39. CAST(RAND() * 1000 AS DECIMAL(18, 0)),
  40. CAST(RAND() * 10 AS INT) + 1,
  41. GEOGRAPHY::Point(@latitude, @longitude, 4326),
  42. @point,
  43. @device_hierarchy,
  44. @device_description
  45. );
  46.  
  47. SET @i = @i + 1;
  48. END;
  49.  
  50.  
  51. -- Tworzenie hierarchii dla pierwszego urządzenia
  52. DECLARE @root HIERARCHYID;
  53. SET @root = HIERARCHYID::GetRoot();
  54. DECLARE @child HIERARCHYID;
  55. SET @child = @root.GetDescendant(NULL, NULL);
  56.  
  57. -- Aktualizacja wartości w kolumnie device_hierarchy
  58. UPDATE Renewable_Energy_Devices
  59. SET device_hierarchy = @child
  60. WHERE device_id = 'Device_1';
  61.  
  62. -- Aktualizacja wartości w kolumnie device_hierarchy dla wartości między 2 a 50
  63. DECLARE @j INT = 2;
  64. WHILE @j <= 50
  65. BEGIN
  66. SET @child = @child.GetDescendant(NULL, NULL);
  67.  
  68. UPDATE Renewable_Energy_Devices
  69. SET device_hierarchy = @child
  70. WHERE device_id = 'Device_' + CAST(@j AS NVARCHAR(5));
  71.  
  72. SET @j = @j + 1;
  73. END;
  74.  
  75. -- Wyświetlenie zaktualizowanych rekordów
  76. -- SELECT * FROM Renewable_Energy_Devices;
  77.  
  78.  
  79.  
  80.  
  81. WITH DeviceHierarchyCTE AS (
  82. SELECT
  83. device_id,
  84. device_hierarchy,
  85. CAST('/' + device_id + '/' AS NVARCHAR(MAX)) AS hierarchy_path,
  86. CAST('/' + device_id + '/' AS NVARCHAR(MAX)) AS device_path,
  87. 1 AS hierarchy_level
  88. FROM
  89. Renewable_Energy_Devices
  90. WHERE
  91. device_hierarchy = HIERARCHYID::GetRoot()
  92.  
  93. UNION ALL
  94.  
  95. SELECT
  96. red.device_id,
  97. red.device_hierarchy,
  98. dhc.hierarchy_path + red.device_id + '/' AS hierarchy_path,
  99. dhc.device_path + red.device_id + '/' AS device_path,
  100. dhc.hierarchy_level + 1
  101. FROM
  102. Renewable_Energy_Devices AS red
  103. INNER JOIN
  104. DeviceHierarchyCTE AS dhc ON dhc.device_hierarchy.IsDescendantOf(red.device_hierarchy) = 1
  105. )
  106. SELECT
  107. device_id,
  108. hierarchy_path,
  109. device_path,
  110. hierarchy_level
  111. FROM
  112. DeviceHierarchyCTE
  113. ;
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement